• 【C# 基础应用】我的第一个App,不容易——随机生成小人网站,asp.net core


    Index page

    Welcome page

    生成很多不同的小人哦~我是如何实现这么stupid but interesting的程序呢?我用了ASP.NET Core

    画小人的话,用了一个很stupid的辅助类, 自己写的,小人脸宽21,然后鼻子占1,剩下的眼睛,鼻子,脸,耳朵,分分看。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading;
     6 using System.Threading.Tasks;
     7 
     8 namespace game1.Controllers
     9 {
    10     public class DrawingHelper
    11     {
    12         public static string randomEye = @"-|@!~^+'o0O⊙";
    13         public static string randomMouth = @"_x*oO~DVv";
    14         public static string randomHair = randomEye + randomMouth;
    15         public static List<StringBuilder> Draw(int FaceCount)
    16         {
    17             int RowWidth = 21;
    18             int hairWidth = 15;
    19             
    20             string leftEar = "(";
    21             string rightEar = ")";
    22             string leftFace = "|";
    23             string nose = "*";
    24 
    25             var faces = new List<StringBuilder>();
    26             
    27             for (int t = 1; t <= FaceCount; t++)
    28             {
    29                 StringBuilder sb = new StringBuilder();
    30                 string eye = GetRandomPart(randomEye, 1);
    31                 string mouth = GetRandomPart(randomMouth, 1);
    32                 string hairStyle = GetRandomPart(randomHair, 1);
    33                 DrawHair(RowWidth, hairWidth, hairStyle,sb);
    34                 DrawCartoon(leftEar, rightEar, leftFace, eye, nose, mouth,sb);
    35                 sb.Append("
    ");
    36                 faces.Add(sb);
    37             }
    38             return faces;
    39             
    40         }
    41 
    42         private static void DrawHair(int RowWidth, int hairWidth, string hairStyle,StringBuilder sb)
    43         {
    44             StringBuilder hair = new StringBuilder();
    45             for (int i = 1; i <= hairWidth - 2; i++)
    46             {
    47                 hair.Append(hairStyle);
    48             }
    49             sb.AppendLine((" " + hair + " ").PadLeft((RowWidth - hairWidth) / 2 + hairWidth, ' '));
    50             sb.AppendLine(("|" + hair + "|").PadLeft((RowWidth - hairWidth) / 2 + hairWidth, ' '));
    51         }
    52         public static string GetRandomPart(string pwdchars, int pwdlen)
    53         {
    54             
    55             string tmpstr = "";
    56             int iRandNum;
    57             Thread.Sleep(100);
    58             long tick = DateTime.Now.Ticks;
    59             Random rnd = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
    60             for (int i = 0; i < pwdlen; i++)
    61             {
    62                 iRandNum = rnd.Next(pwdchars.Length);
    63                 tmpstr += pwdchars[iRandNum];
    64             }
    65             return tmpstr;
    66         }
    67         private static void DrawCartoon(string leftEar, string rightEar, string leftFace, string eye, string nose, string mouth, StringBuilder sb)
    68         {
    69             //Print left
    70             PrintLeftParts(leftEar, 3,sb);
    71             PrintLeftParts(leftFace, 0,sb);
    72             PrintLeftParts(eye, 2,sb);
    73             PrintLeftParts(nose, 2,sb);
    74             //Pring right
    75             PrintLeftParts(eye, 2, sb);
    76             PrintLeftParts(leftFace, 2,sb);
    77             PrintLeftParts(rightEar, 0,sb);
    78             //Print down face
    79             sb.AppendLine();
    80             PrintLeftParts(leftFace, 4,sb);
    81             PrintLeftParts(mouth, 5,sb,'_');
    82             PrintLeftParts(leftFace, 5, sb,'_');
    83             sb.AppendLine();
    84             sb.AppendLine();
    85 
    86         }
    87 
    88         static void PrintLeftParts(string organName, int leftPad, StringBuilder sb,char paddingchar = ' ')
    89         {
    90             for( int i=0; i < leftPad; i++)
    91             {
    92                 sb.Append(paddingchar);
    93             }
    94             sb.Append(organName);
    95         }
    96     }
    97 }

    上一个HelloWorldController,也是整个程序唯一的controller。

    using game1.Models;
    using Microsoft.ApplicationInsights;
    using Microsoft.AspNetCore.Mvc;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.Encodings.Web;
    
    // For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
    
    namespace game1.Controllers
    {
        public class HelloWorldController : Controller
        {
            TelemetryClient tc = new TelemetryClient();
            // 
            // GET: /HelloWorld/
    
            [HttpPost]
            public ActionResult Index(string userName, int count)
            {
                FaceInfo fi = new FaceInfo();
                fi.Name = userName;
                fi.FaceCount = count;
                return RedirectToAction("Welcome",fi);
            }
            [HttpGet]
            public ActionResult Index()
            {
    
                return View();
            }
            public ActionResult About()
            {
    
                return View();
            }
            // 
            // GET: /HelloWorld/Welcome/ 
    
            //public string Welcome(string name, int ID = 1)
            //{
            //    tc.TrackEvent("welcome");
            //    return HtmlEncoder.Default.Encode($"Hello {name}, ID: {ID}");
            //}
            public ActionResult Welcome(FaceInfo fi)
            {
                tc.TrackEvent("Generating....");
                ViewData["Message"] = "Hello " + fi.Name;
                ViewData["FaceCount"] = fi.FaceCount;
                List<String> faces = DrawingHelper.Draw(fi.FaceCount).Select(t => t.ToString()).ToList();
                return View(faces);
            }
        }
    }

    omg~我还有一个model呢~ model好像是实现了在controller里传值。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace game1.Models
    {
        public class FaceInfo
        {
            public int FaceCount { get; set; }
            public string Name { get; set; }
        }
    }

    Index.cshtml里很简单

    俩输入框,一个按钮

    @model game1.Models.FaceInfo
    @{
        ViewData["Title"] = "Index";
    }
    
    <h2>--------------HELLO--------------</h2>
    <hr />
    @using (Html.BeginForm())
    {
        <p>User name:</p>
        <input type="text" name="userName" />
        <p>Face count:</p>
        <input type="text" name="count" />
        <p></p>
        <input type="submit" value="Try It!"/>
    }>
    
    <p>Hello to many little faces!</p>

    Welcome.cshtml似乎更简单!

    @{
        ViewData["Title"] = "Welcome";
    }
    @model List<String>
    <h2>Welcome to Winnie's app</h2>
    
    <p>This is my first web app.</p>
    
    @foreach(var element in Model)
    {
        <li>@ViewData["Message"]</li>
        <pre>@Html.DisplayFor(m=>element)</pre>
    }

    嗯虽然简单,但是宝宝也忙乎了一天~

  • 相关阅读:
    PHP递归复制文件夹以及传输文件夹到其他服务器。
    CSS中的disable,hidden,readonly
    linux下用scp命令在两个服务器之间传输文件,利用php_scp函数进行文件传输
    在PHP中调用php_ssh实现远程登陆linux服务器并执行shell脚本。
    Centos7搭建lamp环境
    Golang 标准库构建 http.Requests 请求
    go modules 的使用
    Golang 自用第三方库
    《Go 语言并发之道》读后感-第五章
    《Go 语言并发之道》读后感
  • 原文地址:https://www.cnblogs.com/jin-wen-xin/p/6101008.html
Copyright © 2020-2023  润新知