sql server 关于表中只增标识问题
由于我们系统时间用的过长,数据量大,设计是采用自增ID
我们插入数据的时候把ID也写进去,我们可以采用 关闭和开启自增标识
没有关闭的时候 ,提示一下错误,不能修改
set identity_insert test on 关闭标识 在添加时候 提示成功
set identity_insert 表名 on 关闭
set identity_insert 表名 off 开启
C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
string file =Application.StartupPath+ @"\WinFrm_Main.exe" ; //运行程序位置
public Form1()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e)
{
// StopExe(file);
timer1.Enabled = false ;
}
private void button2_Click( object sender, EventArgs e)
{
timer1.Enabled = true ;
}
void StopExe( string file)
{
Process pr = new Process(); //声明一个进程类对象
pr.StartInfo.FileName = file; //指定运行的程序,
pr.Start(); //运行
}
void StartExt()
{
//Process[] p_arry = Process.GetProcesses();//得到系统所有进程
//for (int i = 0; i < p_arry.Length; i++)//遍历每个进程
//{
// if (p_arry[i].ProcessName == "WinFrm_Main" && !p_arry[i].Responding)//发现有名为QQ的进程
// {
// p_arry[i].Kill();//就结束它。
// return;
// }
//}
Process[] proc = Process.GetProcessesByName( "WinFrm_Main" ); //创建一个进程数组,把与此进程相关的资源关联。
for ( int i = 0; i < proc.Length; i++)
{
proc[i].Kill(); //逐个结束进程.
}
System.GC.Collect(); //垃圾回收
}
private void timer1_Tick( object sender, EventArgs e)
{
Process[] proc = Process.GetProcessesByName( "WinFrm_Main" ); //创建一个进程数组,把与此进程相关的资源关联。
for ( int i = 0; i < proc.Length; i++)
{
if (proc[i].ProcessName == "WinFrm_Main" && !proc[i].Responding) //发现有名为WinFrm_Main的进程 或 WinFrm_Main和电脑停止交互 就退出
{
proc[i].Kill(); //就结束它。
//StopExe(file);
}
}
if (proc.Length==0) //启动
StopExe(file);
System.GC.Collect(); //垃圾回收
}
|
下载地址:http://download.****.net/detail/amy1314250/9849891
ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例
HTML代码 和js 代码
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <script src="~/js/jquery-1.8.3.min.js"></script> <script src="~/js/ajaxfileupload.js"></script> <script type="text/javascript"> $(function () { $("#butLoad").click(function () { $("#img1").attr("src", "../images/timg.gif"); //调用action $.ajaxFileUpload({ url: "../Upload/UpLoad", secureuri: false, //一般设置为false fileElementId: 'Img', //文件上传空间的id属性 <input type="file" id="Img" name="file" /> dataType: 'json', //返回值类型 success: function (data, status) //服务器成功响应处理函数 { $("#img1").attr("src", data.imgurl); if (typeof (data.error) != 'undefined') { if (data.error != '') { alert(data.error); } else { alert(data.msg); } } }, error: function (data, status, e)//服务器响应失败处理函数 { alert(e); } }); }); $("#butLoadAsp").click(function () { $("#imgAsp").attr("src", "../images/timg.gif"); //调用aspx $.ajaxFileUpload({ url: "../Ajax/UpLoad.aspx?__Action=UpLoadImg", secureuri: false, //一般设置为false fileElementId: 'ImgAsp', //文件上传空间的id属性 <input type="file" id="Img" name="file" /> dataType: 'json', //返回值类型 success: function (data, status) //服务器成功响应处理函数 { $("#imgAsp").attr("src", data.imgurl); if (typeof (data.error) != 'undefined') { if (data.error != '') { alert(data.error); } else { alert(data.msg); } } }, error: function (data, status, e)//服务器响应失败处理函数 { alert(e); } }); }); }); function ChImages(obj) { $("#img1").attr("src", obj.value) } </script> </head> <body> <div> <h3>mvc-ajax</h3> <input type="file" id="Img" name="file" onchange="ChImages(this)" /> @*注意:name一定要写*@ <button id="butLoad">上传</button> <img src="" id="img1" alt="请选择图片" width="200" /> </div> <div> <h3>asp.net-ajax</h3> <input type="file" id="ImgAsp" name="file" /> @*注意:name一定要写*@ <button id="butLoadAsp">上传</button> <img src="" id="imgAsp" alt="请选择图片" width="200" /> </div> </body> </html>
mvc 控制中代码
[HttpPost]//过滤 public JsonResult UpLoad() { HttpFileCollectionBase files = Request.Files;//这里只能用<input type="file" />才能有效果,因为服务器控件是HttpInputFile类型 object result = new { error="error", msg="上传失败",imgurl= files[0].FileName}; string msg = string.Empty; string error = string.Empty; string imgurl; if (files.Count > 0) { string savePath = Server.MapPath("/") + "UpLoadImg\\";//保存文件地址 //string saveDir = System.Web.HttpContext.Current.Server.MapPath(savePath); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } files[0].SaveAs(savePath + System.IO.Path.GetFileName(files[0].FileName)); msg = " 成功! 文件大小为:" + files[0].ContentLength; imgurl = "../UpLoadImg/" + files[0].FileName; result =new { error="success", msg= msg, imgurl=imgurl }; } return Json(result, "text/html"); }
aspx.cs 代码
public partial class UpLoad : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string action = Request["__Action"]; if (action==null || action == string.Empty) return; Page p = this; Type pageType = p.GetType(); MethodInfo method = pageType.GetMethod(action); if (method != null) method.Invoke(p, null); } public void UpLoadImg() { HttpFileCollection files = Request.Files;//这里只能用<input type="file" />才能有效果,因为服务器控件是HttpInputFile类型 // object result = new { error = "error", msg = "上传失败", imgurl = files[0].FileName }; string result = "{ error:'error', msg:'上传失败',imgurl:'" + files[0].FileName + "'}"; string msg = string.Empty; string error = string.Empty; string imgurl; if (files.Count > 0) { string savePath = Server.MapPath("/") + "UpLoadImg\\";//保存文件地址 //string saveDir = System.Web.HttpContext.Current.Server.MapPath(savePath); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } files[0].SaveAs(savePath + System.IO.Path.GetFileName(files[0].FileName)); msg = " 成功! 文件大小为:" + files[0].ContentLength; imgurl = "../UpLoadImg/" + files[0].FileName; result = "{ error:'" + error + "', msg:'" + msg + "',imgurl:'" + imgurl + "'}"; } Response.Clear(); Response.Write(result.ToString()); Response.End(); } }
MVC和aspx 有些不同,MVC获取HttpInputFile 用HttpFileCollectionBase 类,aspx获取HttpInputFile 用HttpFileCollection 类
个人学习,请多多指教
代码:http://files.cnblogs.com/files/BensonHai/UploadImage.rar 本人是用VS2015写的
将小写阿拉伯数字转换成大写的汉字,
#region 小写转大写
/// <summary> ///
/// 金额转为大写金额
/// </summary>
/// <param name="LowerMoney"></param> <returns></returns>
public static string MoneyToChinese(decimal NubMoney)
{
string LowerMoney = NubMoney.ToString();
string functionReturnValue = null;
bool IsNegative = false;
// 是否是负数
if (LowerMoney.Trim().Substring(0, 1) == "-")
{ // 是负数则先转为正数
LowerMoney = LowerMoney.Trim().Remove(0, 1);
IsNegative = true;
}
string strLower = null;
string strUpart = null; string strUpper = null; int iTemp = 0;
// 保留两位小数 123.489→123.49 123.4→123.4
LowerMoney = Math.Round(double.Parse(LowerMoney), 2).ToString();
if (LowerMoney.IndexOf(".") > 0)
{
if (LowerMoney.IndexOf(".") == LowerMoney.Length - 2)
{ LowerMoney = LowerMoney + "0"; }
}
else
{
LowerMoney = LowerMoney + ".00";
} strLower = LowerMoney; iTemp = 1; strUpper = ""; while (iTemp <= strLower.Length)
{
switch (strLower.Substring(strLower.Length - iTemp, 1))
{
case ".": strUpart = "圆";
break;
case "0": strUpart = "零"; break;
case "1": strUpart = "壹";
break;
case "2": strUpart = "贰"; break;
case "3": strUpart = "叁";
break;
case "4": strUpart = "肆"; break;
case "5": strUpart = "伍";
break;
case "6": strUpart = "陆"; break;
case "7": strUpart = "柒";
break;
case "8": strUpart = "捌"; break;
case "9": strUpart = "玖";
break;
} switch (iTemp)
{
case 1: strUpart = strUpart + "分";
break;
case 2: strUpart = strUpart + "角"; break;
case 3:
strUpart = strUpart + ""; break;
case 4: strUpart = strUpart + "";
break;
case 5: strUpart = strUpart + "拾"; break;
case 6:
strUpart = strUpart + "佰"; break;
case 7: strUpart = strUpart + "仟";
break;
case 8: strUpart = strUpart + "万"; break;
case 9:
strUpart = strUpart + "拾"; break;
case 10:
strUpart = strUpart + "佰"; break;
case 11:
strUpart = strUpart + "仟"; break;
case 12:
strUpart = strUpart + "亿"; break;
case 13:
strUpart = strUpart + "拾"; break;
case 14:
strUpart = strUpart + "佰"; break;
case 15:
strUpart = strUpart + "仟"; break;
case 16:
strUpart = strUpart + "万"; break;
default:
strUpart = strUpart + ""; break;
}
strUpper = strUpart + strUpper; iTemp = iTemp + 1;
} strUpper = strUpper.Replace("零拾", "零");
strUpper = strUpper.Replace("零佰", "零"); strUpper = strUpper.Replace("零仟", "零"); strUpper = strUpper.Replace("零零零", "零");
strUpper = strUpper.Replace("零零", "零"); strUpper = strUpper.Replace("零角零分", "整"); strUpper = strUpper.Replace("零分", "整");
strUpper = strUpper.Replace("零角", "零"); strUpper = strUpper.Replace("零亿零万零圆", "亿圆");
strUpper = strUpper.Replace("亿零万零圆", "亿圆"); strUpper = strUpper.Replace("零亿零万", "亿");
strUpper = strUpper.Replace("零万零圆", "万圆"); strUpper = strUpper.Replace("零亿", "亿");
strUpper = strUpper.Replace("零万", "万"); strUpper = strUpper.Replace("零圆", "圆");
strUpper = strUpper.Replace("零零", "零");
// 对壹圆以下的金额的处理
if (strUpper.Substring(0, 1) == "圆")
{
strUpper = strUpper.Substring(1, strUpper.Length - 1);
} if (strUpper.Substring(0, 1) == "零")
{
strUpper = strUpper.Substring(1, strUpper.Length - 1);
}
if (strUpper.Substring(0, 1) == "角")
{
strUpper = strUpper.Substring(1, strUpper.Length - 1);
} if (strUpper.Substring(0, 1) == "分")
{
strUpper = strUpper.Substring(1, strUpper.Length - 1);
} if (strUpper.Substring(0, 1) == "整")
{
strUpper = "零圆整";
} functionReturnValue = strUpper; if (IsNegative == true)
{
return "负" + functionReturnValue;
}
else { return functionReturnValue; }
}
/// <summary> ///
/// 金额转为大写金额
/// </summary>
/// <param name="LowerMoney"></param> <returns></returns>
public static string NumberToChinese(decimal NubMoney)
{
string LowerMoney = NubMoney.ToString();
string functionReturnValue = null;
bool IsNegative = false;
// 是否是负数
if (LowerMoney.Trim().Substring(0, 1) == "-")
{ // 是负数则先转为正数
LowerMoney = LowerMoney.Trim().Remove(0, 1);
IsNegative = true;
}
string strLower = null;
string strUpart = null; string strUpper = null; int iTemp = 0;
// 保留两位小数 123.489→123.49 123.4→123.4
LowerMoney = Math.Round(double.Parse(LowerMoney), 2).ToString();
if (LowerMoney.IndexOf(".") > 0)
{
if (LowerMoney.IndexOf(".") == LowerMoney.Length - 2)
{ LowerMoney = LowerMoney + "0"; }
}
else
{
LowerMoney = LowerMoney + ".00";
} strLower = LowerMoney; iTemp = 1; strUpper = ""; while (iTemp <= strLower.Length)
{
switch (strLower.Substring(strLower.Length - iTemp, 1))
{
case ".": strUpart = "";
break;
case "0": strUpart = "零"; break;
case "1": strUpart = "一";
break;
case "2": strUpart = "二"; break;
case "3": strUpart = "三";
break;
case "4": strUpart = "四"; break;
case "5": strUpart = "五";
break;
case "6": strUpart = "六"; break;
case "7": strUpart = "七";
break;
case "8": strUpart = "八"; break;
case "9": strUpart = "九";
break;
}
switch (iTemp)
{
case 1: strUpart = strUpart + "";
break;
case 2: strUpart = strUpart + ""; break;
case 3:
strUpart = strUpart + ""; break;
case 4: strUpart = strUpart + "";
break;
case 5: strUpart = strUpart + "拾"; break;
case 6:
strUpart = strUpart + "佰"; break;
case 7: strUpart = strUpart + "仟";
break;
case 8: strUpart = strUpart + "万"; break;
case 9:
strUpart = strUpart + "拾"; break;
case 10:
strUpart = strUpart + "佰"; break;
case 11:
strUpart = strUpart + "仟"; break;
case 12:
strUpart = strUpart + "亿"; break;
case 13:
strUpart = strUpart + "拾"; break;
case 14:
strUpart = strUpart + "佰"; break;
case 15:
strUpart = strUpart + "仟"; break;
case 16:
strUpart = strUpart + "万"; break;
default:
strUpart = strUpart + ""; break;
}
strUpper = strUpart + strUpper; iTemp = iTemp + 1;
}
strUpper = strUpper.Replace("零拾", "零");
strUpper = strUpper.Replace("零佰", "零");
strUpper = strUpper.Replace("零仟", "零");
strUpper = strUpper.Replace("零零零", "零");
strUpper = strUpper.Replace("零零", "零");
strUpper = strUpper.Replace("零角零分", "整");
strUpper = strUpper.Replace("零分", "整");
strUpper = strUpper.Replace("零角", "零");
strUpper = strUpper.Replace("零亿零万零圆", "亿圆");
strUpper = strUpper.Replace("亿零万零圆", "亿圆");
strUpper = strUpper.Replace("零亿零万", "亿");
strUpper = strUpper.Replace("零万零圆", "万圆");
strUpper = strUpper.Replace("零亿", "亿");
strUpper = strUpper.Replace("零万", "万");
if (strUpper.IndexOf("一亿万") > -1)
strUpper = strUpper.Replace("万","");
strUpper = strUpper.Replace("零圆", "圆");
strUpper = strUpper.Replace("零零", "零");
// 对壹圆以下的金额的处理
if (strUpper.Substring(0, 1) == "圆")
{
strUpper = strUpper.Substring(1, strUpper.Length - 1);
} if (strUpper.Substring(0, 1) == "零")
{
strUpper = strUpper.Substring(1, strUpper.Length - 1);
}
if (strUpper.Substring(0, 1) == "角")
{
strUpper = strUpper.Substring(1, strUpper.Length - 1);
} if (strUpper.Substring(0, 1) == "分")
{
strUpper = strUpper.Substring(1, strUpper.Length - 1);
} if (strUpper.Substring(0, 1) == "整")
{
strUpper = "零圆整";
}
if (strUpper.LastIndexOf("零") > -1) {
strUpper = strUpper.Substring(0, strUpper.Length - 1);
}
functionReturnValue = strUpper;
if (IsNegative == true)
{
return "负" + functionReturnValue;
}
else { return functionReturnValue; }
}
#endregion
C# WinForm 中英文实现, 国际化实现的简单方法
来源:http://www.jb51.net/article/45675.htm,今天看到了借鉴过了,保存一下,下次开发直接用嘻嘻
软件行业发展到今天,国际化问题一直都占据非常重要的位置,而且应该越来越被重视。对于开发人员而言,在编写程序之前,国际化问题是首先要考虑的一个问题,也许有时候这个问题已经在设计者的考虑范围之内,但终归要开发人员去做实现的。因此,如何实现国际化,是开发人员必须掌握的一项基本技能。
今天,这里要讲的就是,在利用C#进行WinForm开发时,国际化是怎么实现的。鉴于时间及篇幅关系,这里仅仅介绍一种简单的国际化实现方法,可能这里提到的方法已经有非常多人提到过,但笔者还是不厌其烦地介绍一下。
要在C#中实现国际化,需要相关资源文件,比如要在一个软件中支持英文、中文两种语言,那么就必须有这两种语言的资源文件,这在C#中可以采用资源文件(后缀名为.resx)来实现,我们不妨定义英文资源文件名称为Resource.en-US,中文资源文件名称为Resource.zh-CN,两种资源文件所涉及的ID都应该是一样的(这对于其他更多的资源文件均是一样的),只不过是展示的名称不同罢了。
有了这两种资源文件,接下来就要考虑如何做的问题了。为了适应多处使用的情形,这里笔者单独编写了一个类ResourceCulture,该类包含了一些静态方法,主要作用是用来设置当前语言及返回当前的语言的相关字符串。该类代码如下:
using System.Reflection;
using System.Resources;
using System.Threading;
using System.Globalization;
namespace GlobalizationTest
{
class ResourceCulture
{
/// <summary>
/// Set current culture by name
/// </summary>
/// <param name="name">name</param>
public static void SetCurrentCulture(string name)
{
if (string.IsNullOrEmpty(name))
{
name = "en-US";
}
Thread.CurrentThread.CurrentCulture = new CultureInfo(name);
}
/// <summary>
/// Get string by id
/// </summary>
/// <param name="id">id</param>
/// <returns>current language string</returns>
public static string GetString(string id)
{
string strCurLanguage = "";
try
{
ResourceManager rm = new ResourceManager("GlobalizationTest.Resource", Assembly.GetExecutingAssembly());
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
strCurLanguage = rm.GetString(id, ci);
}
catch
{
strCurLanguage = "No id:" + id + ", please add.";
}
return strCurLanguage;
}
}
}
在Form1中的代码如下:
/**
* This project is just a example to show how to do the globalization in C# winform.
* You and rebuild and/or modify it by yourself if you want.
* Specially, this project was created in Visual Studio 2010.
*
* Project Name : GlobalizationTest
* Create Date : April 29th, 2010
* */
using System;
using System.Windows.Forms;
namespace GlobalizationTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Set the resource culture
/// </summary>
private void SetResourceCulture()
{
// Set the form title text
this.Text = ResourceCulture.GetString("Form1_frmText");
// Set the groupbox text
this.gbLanguageView.Text = ResourceCulture.GetString("Form1_gbLanguageViewText");
this.gbLanguageSelection.Text = ResourceCulture.GetString("Form1_gbLanguageSelectionText");
// Set the label text
this.lblCurLanguageText.Text = ResourceCulture.GetString("Form1_lblCurLanguageText");
this.lblNameText.Text = ResourceCulture.GetString("Form1_lblNameText");
this.lblPhoneText.Text = ResourceCulture.GetString("Form1_lblPhoneText");
// Set the button text
this.btnMsgShow.Text = ResourceCulture.GetString("Form1_btnMsgShowText");
// Set radiobutton text
this.rbEnglish.Text = ResourceCulture.GetString("Language_EnglishText");
this.rbChinese.Text = ResourceCulture.GetString("Language_ChineseText");
// Set the current language text
if (rbEnglish.Checked)
{
this.lblCurLanguage.Text = ResourceCulture.GetString("Language_EnglishText");
}
else if (rbChinese.Checked)
{
this.lblCurLanguage.Text = ResourceCulture.GetString("Language_ChineseText");
}
}
private void Form1_Load(object sender, EventArgs e)
{
// Set the default language
ResourceCulture.SetCurrentCulture("en-US");
this.SetResourceCulture();
}
private void btnMsgShow_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(txtName.Text))
{
MessageBox.Show(ResourceCulture.GetString("Form1_msgbox_nameText"), ResourceCulture.GetString("Form1_msgbox_TitleText"),
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (string.IsNullOrEmpty(txtPhone.Text))
{
MessageBox.Show(ResourceCulture.GetString("Form1_msgbox_phoneText"), ResourceCulture.GetString("Form1_msgbox_TitleText"),
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
MessageBox.Show(ResourceCulture.GetString("Form1_msgbox_InfoText") + txtName.Text + ", " + txtPhone.Text,
ResourceCulture.GetString("Form1_msgbox_TitleText"), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void rbEnglish_CheckedChanged(object sender, EventArgs e)
{
ResourceCulture.SetCurrentCulture("en-US");
this.SetResourceCulture();
}
private void rbChinese_CheckedChanged(object sender, EventArgs e)
{
ResourceCulture.SetCurrentCulture("zh-CN");
this.SetResourceCulture();
}
}
}
最终的效果如下图1和图2所示:
图1
归结起来,要在C#的WinForm中实现国际化,至少需要做好以下几点:
(1)准备所需资源文件(如本文中提到的英文和中文资源文件);
(2)引入命名空间(包括:System.Reflection、System.Resources、System.Threading和System.Globalization);
(3)实例化资源管理器(即ResourceManager);
(4)设置当前进程的语言区域;
(5)通过资源管理器从指定的资源文件中获取所需值。
通过上述的方法即可简单实现国际化。
ASP.NET Core 2 学习笔记(三)
之前ASP.NET中使用的HTTP Modules及HTTP Handlers,在ASP.NET Core中已不复存在,取而代之的是Middleware。Middleware除了简化了HTTP Modules/Handlers的使用方式,还带入了Pipeline的概念。
本篇将介绍ASP.NET Core的Middleware概念及用法。
Middleware 概念
ASP.NET Core在Middleware的官方说明中,使用了Pipeline这个名词,意指Middleware像水管一样可以串联在一起,所有的Request及Response都会层层经过这些水管。
用图例可以很容易理解,如下图:
App.Use
Middleware的注册方式是在Startup.cs的Configure
对IApplicationBuilder
使用Use
方法注册。
大部分扩展的Middleware也都是以Use开头的方法注册,例如:
- UseMvc():MVC的Middleware
- UseRewriter():URL rewriting的Middleware
一个简单的Middleware 范例。如下:
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace MyWebsite
{ public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Use(async (context, next) =>
{
await context.Response.WriteAsync( "First Middleware in. \r\n" );
await next.Invoke();
await context.Response.WriteAsync( "First Middleware out. \r\n" );
});
app.Use(async (context, next) =>
{
await context.Response.WriteAsync( "Second Middleware in. \r\n" );
await next.Invoke();
await context.Response.WriteAsync( "Second Middleware out. \r\n" );
});
app.Use(async (context, next) =>
{
await context.Response.WriteAsync( "Third Middleware in. \r\n" );
await next.Invoke();
await context.Response.WriteAsync( "Third Middleware out. \r\n" );
});
app.Run(async (context) =>
{
await context.Response.WriteAsync( "Hello World! \r\n" );
});
}
}
} |
用浏览器打开网站任意连结,输出结果:
1
2
3
4
5
6
7
|
First Middleware in .
Second Middleware in .
Third Middleware in .
Hello World! Third Middleware out. Second Middleware out. First Middleware out. |
在Pipeline的概念中,注册顺序是很重要的事情。请求经过的顺序一定是先进后出。
Request 流程如下图:
Middleware 也可以作为拦截使用,如下:
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace MyWebsite
{ public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Use(async (context, next) =>
{
await context.Response.WriteAsync( "First Middleware in. \r\n" );
await next.Invoke();
await context.Response.WriteAsync( "First Middleware out. \r\n" );
});
app.Use(async (context, next) =>
{
await context.Response.WriteAsync( "Second Middleware in. \r\n" );
// 水管阻塞,封包不往后送
var condition = false ;
if (condition)
{
await next.Invoke();
}
await context.Response.WriteAsync( "Second Middleware out. \r\n" );
});
app.Use(async (context, next) =>
{
await context.Response.WriteAsync( "Third Middleware in. \r\n" );
await next.Invoke();
await context.Response.WriteAsync( "Third Middleware out. \r\n" );
});
app.Run(async (context) =>
{
await context.Response.WriteAsync( "Hello World! \r\n" );
});
}
}
} |
输出结果:
1
2
3
4
|
First Middleware in .
Second Middleware in .
Second Middleware out. First Middleware out. |
在Second Middleware 中,因为没有达成条件,所以封包也就不在往后面的水管传送。流程如图:
App.Run
Run
是Middleware的最后一个行为,以上面图例来说,就是最末端的Action。
它不像Use
能串联其他Middleware,但Run
还是能完整的使用Request及Response。
App.Map
Map
是能用来处理一些简单路由的Middleware,可依照不同的URL指向不同的Run
及注册不同的Use
。
新增一个路由如下:
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace MyWebsite
{ public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Use(async (context, next) =>
{
await context.Response.WriteAsync( "First Middleware in. \r\n" );
await next.Invoke();
await context.Response.WriteAsync( "First Middleware out. \r\n" );
});
// app.Use(async (context, next) =>
// {
// await context.Response.WriteAsync("Second Middleware in. \r\n");
// // 水管阻塞,封包不往后送
// var condition = false;
// if (condition)
// {
// await next.Invoke();
// }
// await context.Response.WriteAsync("Second Middleware out. \r\n");
// });
app.Map( "/second" , mapApp =>
{
mapApp.Use(async (context, next) =>
{
await context.Response.WriteAsync( "Second Middleware in. \r\n" );
await next.Invoke();
await context.Response.WriteAsync( "Second Middleware out. \r\n" );
});
mapApp.Run(async context =>
{
await context.Response.WriteAsync( "Second. \r\n" );
});
});
app.Use(async (context, next) =>
{
await context.Response.WriteAsync( "Third Middleware in. \r\n" );
await next.Invoke();
await context.Response.WriteAsync( "Third Middleware out. \r\n" );
});
app.Run(async (context) =>
{
await context.Response.WriteAsync( "Hello World! \r\n" );
});
}
}
} |
开启网站任意连结,会显示:
1
2
3
4
5
|
First Middleware in .
Third Middleware in .
Hello World! Third Middleware out. First Middleware out. |
开启网站http://localhost:5000/second
,则会显示:
1
2
3
4
5
|
First Middleware in .
Second Middleware in .
Second. Second Middleware out. First Middleware out. |
创建Middleware 类
如果Middleware全部都写在Startup.cs,代码将很难维护,所以应该把自定义的Middleware逻辑独立出来。
建立Middleware类不需要额外继承其它类或接口,一般的类即可,例子如下:
FirstMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace MyWebsite
{ public class FirstMiddleware
{
private readonly RequestDelegate _next;
public FirstMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
await context.Response.WriteAsync($ "{nameof(FirstMiddleware)} in. \r\n" );
await _next(context);
await context.Response.WriteAsync($ "{nameof(FirstMiddleware)} out. \r\n" );
}
}
} |
全局注册
在Startup.Configure
注册Middleware就可以套用到所有的Request。如下:
Startup.cs
1
2
3
4
5
6
7
8
9
10
|
// ... public class Startup
{ // ...
public void Configure(IApplicationBuilder app)
{
app.UseMiddleware<FirstMiddleware>();
// ...
}
} |
局部注册
Middleware 也可以只套用在特定的Controller 或Action。注册方式如下:
Controllers\HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
|
// .. [MiddlewareFilter( typeof (FirstMiddleware))]
public class HomeController : Controller
{ // ...
[MiddlewareFilter( typeof (SecondMiddleware))]
public IActionResult Index()
{
// ...
}
} |
Extensions
大部分扩展的Middleware都会用一个静态方法包装,如:UseMvc()
、UseRewriter()
等。
自定义的Middleware当然也可以透过静态方法包,范例如下:
Extensions\CustomMiddlewareExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
|
using Microsoft.AspNetCore.Builder;
namespace MyWebsite
{ public static class CustomMiddlewareExtensions
{
public static IApplicationBuilder UseFirstMiddleware( this IApplicationBuilder builder)
{
return builder.UseMiddleware<FirstMiddleware>();
}
}
} |
注册Extension Middleware 的方式如下:
Startup.cs
1
2
3
4
5
6
7
8
9
10
|
// ... public class Startup
{ // ...
public void Configure(IApplicationBuilder app)
{
app.UseFirstMiddleware();
// ...
}
} |
参考
ASP.NET Core Middleware Fundamentals
Creating Custom Middleware In ASP.Net Core
老司机发车啦:https://github.com/SnailDev/SnailDev.NETCore2Learning
ASP.NET Core 2 学习笔记(六)
ASP.NET Core MVC跟ASP.NET MVC观念是一致的,使用上也没有什么太大的变化。之前的ASP.NET MVC把MVC及Web API的套件分开,但在ASP.NET Core中MVC及Web API用的套件是相同的。
本篇将介绍ASP.NET Core MVC设置方式。
MVC 简介
ASP.NET Core的MVC(Model-View-Controller)架构模式延续ASP.NET MVC,把网站分成三大元件Model、View及Controller,依赖关系如下图:
- Model
负责数据处理,包含数据存取、业务逻辑、定义数据对象及验证数据。 - View
负责UI显示,如HTML、CSS等界面设计配置。 - Controller
负责将使用者Requset找到相对应的Model及View,做为控制流程的角色。
在ASP.NET Core中使用MVC或Web API,需要
Microsoft.AspNetCore.Mvc
套件。
注册MVC 服务
在Startup.cs的ConfigureServices
加入MVC的服务,并在Configure
对IApplicationBuilder
使用UseMvcWithDefaultRoute
方法注册MVC预设路由的Middleware。如下:
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// ... public class Startup
{ public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseMvcWithDefaultRoute();
}
} |
- UseMvcWithDefaultRoute
这个是ASP.NET Core的预设路由,会将Request来的URL找到对应的Controller及Action。
MVC 示例
Model
建立一个简单的Model 用于Controller 跟View 互动。
Models\UserModel.cs
1
2
3
4
5
6
7
8
|
namespace MyWebsite.Models
{ public class UserModel
{
// 名称
public string Name { get ; set ; } = "SnailDev" ;
}
} |
Controller
在项目目录下建立一个Controllers文件夹,把Controller都放这个目录。
过去ASP.NET把MVC及Web API用的Controller分为Controller
及ApiController
,现在ASP.NET Core把两者合一,不再区分ApiController
。
所以要建立一个类,名称后缀Controller即可,如下:
Controllers\HomeController.cs
1
2
3
4
5
6
7
8
9
10
|
namespace MyWebsite.Controllers
{ public class HomeController
{
public string Index()
{
return "this is homecontroller index action" ;
}
}
} |
但要让Controller跟View互动,还是需要继承Controller
比较方便,如下:
Controllers\HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
using Microsoft.AspNetCore.Mvc;
using MyWebsite.Models;
namespace MyWebsite.Controllers
{ public class HomeController : Controller
{
public IActionResult Index()
{
var user = new UserModel();
return View(model: user);
}
}
} |
IActionResult
回传的方式可以有很多种,通过继承Controller
后,就可以使用Controller
的方法:
- View
以上例来说,通过回传View
方法,可以找到该Controller & Action对应的*.cshtml
,并且把UserModel传给View使用。 - HTTP Status Code
响应包含HTTP Status。常用的响应有Ok
、BadRequest
、NotFound
等。
例如:return BadRequest("Internal Server Error")
,会响应HTTP Status 400及Internal Server Error字串。 - Redirect
可以把Request转给其他的Action或URL。转向的方法有Redirect
、LocalRedirect
、RedirectToAction
、RedirectToRoute
等。
例如:return RedirectToAction("Login", "Authentication")
,就会把Request转向到AuthenticationController的Login()。 - Formatted Response
响应时指定Content-Type。Web API的回传通常都用这种方式,序列化对象顺便标注Content-Type。
例如:return Json(user)
,会将对象序列化成JSON字串,并在HTTP Headers带上Content-Type=application/json。
View
View跟Controller有相互的对应关系,预设在Controller使用View
方法回传结果,会从以下目录寻找对应的*.cshtml
:
-
Views\{ControllerName}\{ActionName}.cshtml
寻找与Controller同名的子目录,再找到与Action同名的*.cshtml
。
如上例HomeController.Index()
,就会找项目目录下的Views\Home\Index.cshtml
文件。 -
Views\Shared\{ActionName}.cshtml
如果Controller同名的子目录,找不到Action同名的*.cshtml
。就会到Shared目录找。如上例HomeController.Index()
,就会找项目目录下的Views\Shared\Index.cshtml文件
。
Views\Home\Index.cshtml
1
2
3
|
@model MyWebsite.Models.UserModel Hello~ 我是 @Model.Name |
在*.cshtml
用@model
绑定Model的型别,才可以使用@Model
取得Controller传入的对象。
示例结果
数据流动图如下:
参考
Overview of ASP.NET Core MVC
ASP.NET Core - Setup MVC
老司机发车啦:https://github.com/SnailDev/SnailDev.NETCore2Learning