Index page

Welcome page

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

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace game1.Controllers
{
public class DrawingHelper
{
public static string randomEye = @"-|@!~^+'o0O⊙";
public static string randomMouth = @"_x*oO~DVv";
public static string randomHair = randomEye + randomMouth;
public static List<StringBuilder> Draw(int FaceCount)
{
int RowWidth = ;
int hairWidth = ; string leftEar = "(";
string rightEar = ")";
string leftFace = "|";
string nose = "*"; var faces = new List<StringBuilder>(); for (int t = ; t <= FaceCount; t++)
{
StringBuilder sb = new StringBuilder();
string eye = GetRandomPart(randomEye, );
string mouth = GetRandomPart(randomMouth, );
string hairStyle = GetRandomPart(randomHair, );
DrawHair(RowWidth, hairWidth, hairStyle,sb);
DrawCartoon(leftEar, rightEar, leftFace, eye, nose, mouth,sb);
sb.Append("\n");
faces.Add(sb);
}
return faces; } private static void DrawHair(int RowWidth, int hairWidth, string hairStyle,StringBuilder sb)
{
StringBuilder hair = new StringBuilder();
for (int i = ; i <= hairWidth - ; i++)
{
hair.Append(hairStyle);
}
sb.AppendLine((" " + hair + " ").PadLeft((RowWidth - hairWidth) / + hairWidth, ' '));
sb.AppendLine(("|" + hair + "|").PadLeft((RowWidth - hairWidth) / + hairWidth, ' '));
}
public static string GetRandomPart(string pwdchars, int pwdlen)
{ string tmpstr = "";
int iRandNum;
Thread.Sleep();
long tick = DateTime.Now.Ticks;
Random rnd = new Random((int)(tick & 0xffffffffL) | (int)(tick >> ));
for (int i = ; i < pwdlen; i++)
{
iRandNum = rnd.Next(pwdchars.Length);
tmpstr += pwdchars[iRandNum];
}
return tmpstr;
}
private static void DrawCartoon(string leftEar, string rightEar, string leftFace, string eye, string nose, string mouth, StringBuilder sb)
{
//Print left
PrintLeftParts(leftEar, ,sb);
PrintLeftParts(leftFace, ,sb);
PrintLeftParts(eye, ,sb);
PrintLeftParts(nose, ,sb);
//Pring right
PrintLeftParts(eye, , sb);
PrintLeftParts(leftFace, ,sb);
PrintLeftParts(rightEar, ,sb);
//Print down face
sb.AppendLine();
PrintLeftParts(leftFace, ,sb);
PrintLeftParts(mouth, ,sb,'_');
PrintLeftParts(leftFace, , sb,'_');
sb.AppendLine();
sb.AppendLine(); } static void PrintLeftParts(string organName, int leftPad, StringBuilder sb,char paddingchar = ' ')
{
for( int i=; i < leftPad; i++)
{
sb.Append(paddingchar);
}
sb.Append(organName);
}
}
}

上一个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>
}

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

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

  1. 微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core

    写在前面 在日常开发中,我这边比较熟悉的配置中心有,携程Apollo,阿里Nacos(配置中心,服务治理一体) 之前文章: Asp.Net Core与携程阿波罗(Apollo)的第一次亲密接触 总体来 ...

  2. 创业成本?亲身经历告诉你做一个app要多少钱

    导语:作为一名苦逼的移动互联网创业者,被外行的朋友们问及最多的问题是“做一个网站需要多少钱?”或者“做一个APP需要多少钱?” 作为一名苦逼的移动互联网创业者,被外行的朋友们问及最多的问题是“做一个网 ...

  3. Windows + IIS 环境部署Asp.Net Core App

    环境:Windows Server 2012, IIS 8, Asp.Net Core 1.1. 不少人第一次在IIS中部署Asp.Net Core App的人都会遇到问题,会发现原来的部署方式无法运 ...

  4. ASP.NET Core 基本项目目录结构 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 基本项目目录结构 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 基本项目目录结构 上一章节中我们成功创建了一个名为 Hell ...

  5. 【ASP.NET Core学习】基础

    新建项目时,程序入口调用CreateDefaultBuilder(args),下面是源代码 public static IHostBuilder CreateDefaultBuilder(string ...

  6. ASP.NET Core中app.UseDeveloperExceptionPage和app.UseExceptionHandler方法有什么用

    在新建一个ASP.NET Core项目后,在项目Startup类的Configure方法中默认会添加两个方法的调用,app.UseDeveloperExceptionPage和app.UseExcep ...

  7. 学习ASP.NET Core Blazor编程系列二——第一个Blazor应用程序(下)

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  8. 学习ASP.NET Core Blazor编程系列二——第一个Blazor应用程序(完)

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  9. 如何一秒钟从头构建一个 ASP.NET Core 中间件

    前言 其实地上本没有路,走的人多了,也便成了路. -- 鲁迅 就像上面鲁迅说的那样,其实在我们开发中间件的过程中,微软并没有制定一些策略或者文档来约束你如何编写一个中间件程序, 但是其中却存在者一些最 ...

随机推荐

  1. 2016 Multi-University Training Contest 1 H.Shell Necklace

    Shell Necklace Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  2. tornado 学习笔记8 模板以及UI

          Tornado 包含一个简单.快速而且灵活的模板语言.       Tornado同样可以使用任何其他的python模板语言,虽然没有集成这些模板语言进RequestHandler.ren ...

  3. .NET简谈接口

    自从面向对象开发方式的出现,抽象的概念就开始日新月异的发展,面向对象编程.面向接口编程.面向组件编程等等:这一系列的概念都是软件工程所追求的思想范畴,高类聚低耦合. 今天我要简谈的是面向对象里面非常重 ...

  4. css3动画----实现动态省略号 ...

    <h3>实现省略号点点动,纯css3实现动态省略号</h3>上传中<span class="dot">...</span> [css ...

  5. DEV 等待窗口

    DevExpress.Utils.WaitDialogForm dlg = , )); System.Threading.Thread.Sleep(); dlg.Close();

  6. jpa语句报 org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that position [2] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position

    @Query("SELECT area from Area AS area WHERE area.state=0 AND area.name like %?1% ")Area fi ...

  7. Odoo domain 中的 like, ilike, =like, =ilike 举例说明【转】

    Odoo domain 中的 like, ilike, =like, =ilike 举例说明 Odoo domain 操作符使用场景非常多,很多小伙伴被 like, ilike, =like, =il ...

  8. Ubuntu14.04安装wineqq国际版

       一开始,我在Ubuntu14.04下安装的QQ版本是WineQQ2013SP6-20140102-Longene, 但后来发现这个版本QQ在linux下问题很多,比如不能用键盘输入密码,QQ表情 ...

  9. 如何获取hibernate代理类代理的实际对象实例?

    在hibernate中,通过sql语句查询带clob字段的记录,查出来的结果集是List<HashMap<String,Object>>类型,在调用jackson的接口转为js ...

  10. JS问题汇总

    1.Q:$(this)在js中失效,无法获取当前元素 A​:function()在被调用时this是指向window的,如果要想指向被点击的元素,一般是将this作为参数传入,例如: <div ...