sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭中止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx作后台各写了一个

sql server 关于表中只增标识问题

 

因为咱们系统时间用的过长,数据量大,设计是采用自增IDjavascript

咱们插入数据的时候把ID也写进去,咱们能够采用 关闭和开启自增标识html

没有关闭的时候 ,提示一下错误,不能修改java

 

 


set identity_insert test on  关闭标识 在添加时候 提示成功jquery



set identity_insert 表名  on  关闭git



set identity_insert 表名 off   开启github

 

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.csdn.net/detail/amy1314250/9849891ajax

 

ajaxfileupload插件上传图片功能,用MVC和aspx作后台各写了一个案例

 

HTML代码 和js 代码sql

复制代码
@{
    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 控制中代码json

 

复制代码
[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.csConfigureIApplicationBuilder使用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,把网站分红三大元件ModelViewController,依赖关系以下图:

  • Model
    负责数据处理,包含数据存取、业务逻辑、定义数据对象及验证数据。
  • View
    负责UI显示,如HTML、CSS等界面设计配置。
  • Controller
    负责将使用者Requset找到相对应的Model及View,作为控制流程的角色。

在ASP.NET Core中使用MVC或Web API,须要Microsoft.AspNetCore.Mvc套件。

注册MVC 服务

Startup.csConfigureServices加入MVC的服务,并在ConfigureIApplicationBuilder使用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分为ControllerApiController,如今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。经常使用的响应有OkBadRequestNotFound等。
    例如:return BadRequest("Internal Server Error"),会响应HTTP Status 400及Internal Server Error字串。
  • Redirect
    能够把Request转给其余的Action或URL。转向的方法有RedirectLocalRedirectRedirectToActionRedirectToRoute等。
    例如: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

  1. Views\{ControllerName}\{ActionName}.cshtml
    寻找与Controller同名的子目录,再找到与Action同名的*.cshtml
    如上例HomeController.Index(),就会找项目目录下的Views\Home\Index.cshtml文件。
  2. 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

相关文章
相关标签/搜索