【C# 基础应用】我的第一个App,不容易——随机生成小人网站,asp.net core
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的更多相关文章
- 微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core
写在前面 在日常开发中,我这边比较熟悉的配置中心有,携程Apollo,阿里Nacos(配置中心,服务治理一体) 之前文章: Asp.Net Core与携程阿波罗(Apollo)的第一次亲密接触 总体来 ...
- 创业成本?亲身经历告诉你做一个app要多少钱
导语:作为一名苦逼的移动互联网创业者,被外行的朋友们问及最多的问题是“做一个网站需要多少钱?”或者“做一个APP需要多少钱?” 作为一名苦逼的移动互联网创业者,被外行的朋友们问及最多的问题是“做一个网 ...
- Windows + IIS 环境部署Asp.Net Core App
环境:Windows Server 2012, IIS 8, Asp.Net Core 1.1. 不少人第一次在IIS中部署Asp.Net Core App的人都会遇到问题,会发现原来的部署方式无法运 ...
- ASP.NET Core 基本项目目录结构 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 基本项目目录结构 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 基本项目目录结构 上一章节中我们成功创建了一个名为 Hell ...
- 【ASP.NET Core学习】基础
新建项目时,程序入口调用CreateDefaultBuilder(args),下面是源代码 public static IHostBuilder CreateDefaultBuilder(string ...
- ASP.NET Core中app.UseDeveloperExceptionPage和app.UseExceptionHandler方法有什么用
在新建一个ASP.NET Core项目后,在项目Startup类的Configure方法中默认会添加两个方法的调用,app.UseDeveloperExceptionPage和app.UseExcep ...
- 学习ASP.NET Core Blazor编程系列二——第一个Blazor应用程序(下)
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...
- 学习ASP.NET Core Blazor编程系列二——第一个Blazor应用程序(完)
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...
- 如何一秒钟从头构建一个 ASP.NET Core 中间件
前言 其实地上本没有路,走的人多了,也便成了路. -- 鲁迅 就像上面鲁迅说的那样,其实在我们开发中间件的过程中,微软并没有制定一些策略或者文档来约束你如何编写一个中间件程序, 但是其中却存在者一些最 ...
随机推荐
- win10 系统怎么获取最高管理员权限删除文件
http://www.xitongcity.com/jiaocheng/win8_content_3473.html 很多win8.1系统用户在对磁盘文件进行清理时,经常会遇到“文件夹访问被拒绝,您需 ...
- 浅谈JAVA设计模式
没有万行的代码量,就不要想搞清楚设计模式.目前本人代码量大约在六千五百行,2016年需要继续努力,尽快完成万行之约. 工作之余需要,下面提前简单讨论一下设计模式. 创建型模式,共五种:工厂模式.抽象工 ...
- 诡异的C语言实参求值顺序
学了这么久的C语言,竟然第一次碰到这么诡异的实参求值顺序问题,大跌眼镜.果然阅读面太少了! #include<iostream> void foo(int a, int b, int c) ...
- [转]Nodejs基础中间件Connect
Nodejs基础中间件Connect 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的J ...
- [机器学习] 虚拟机VMware中使用Ubuntu的联网问题
在VMware中安装Ubuntu要解决两个问题: 1.VMware Tools安装使用 2.Ubuntu联网的虚拟机设置 1.VMware Tools安装 它的作用就是使用户可以从物理主机直接往虚拟机 ...
- js获取网页屏幕可视区域高度
document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度document.document ...
- 【整理】深入理解拉格朗日乘子法(Lagrange Multiplier) 和KKT条件
在求解最优化问题中,拉格朗日乘子法(Lagrange Multiplier)和KKT(Karush Kuhn Tucker)条件是两种最常用的方法.在有等式约束时使用拉格朗日乘子法,在有不等约束时使用 ...
- EMF学习,为了实现可扩展可自定义的模型验证 - 各种实现方法学习
自: http://blog.csdn.net/javaman_chen/article/details/6057033 http://www.ibm.com/developerworks/cn/op ...
- VIM键盘映射 (Map)~转载
VIM键盘映射 (Map) 设置键盘映射 使用:map命令,可以将键盘上的某个按键与Vim的命令绑定起来.例如使用以下命令,可以通过F5键将单词用花括号括起来: :map <F5> i{e ...
- Xamarin Android 绑定jar库同时将so库打包进去
1.在创建的Bindings Library项目中,新建Assets目录: 2.Assets目录下再分别创建armeabi,armeabi-v7a,x86三个目录: 3.将so文件分布copy到三个目 ...