在ASP.NET这样的Web应用中,Session是用来保存用户状态的常用手段,不过由于服务器内存空间是有限的,所以Session过期时间设置是很有必要的。在ASP.NET中如何设置Session的过期时间呢,很简单,修改web.config配置。 具体修改方法如下,在web.config中进行如下配置 1 2 3 <system.web> <sessionState mode="InProc" timeout="30"/> </system.web> 在这里指的是Session过期时间为30分钟。也就是说30分钟后如果当前用户没有操作,那么Session就会自动过期了。 from:http://www.cnblogs.com/sjrhero/archive/2010/10/15/1852449.html
View Details首先在Global.asax.cs里增加: protected void Application_PreSendRequestHeaders(object sender, EventArgs e) { HttpContext.Current.Response.Headers.Set("Server", "w3cnet.com"); HttpContext.Current.Response.Headers.Remove("X-AspNet-Version"); HttpContext.Current.Response.Cookies.Remove(".ASPXAUTH"); } 然后web.config的system.webServer节点下增加: <httpProtocol> <customHeaders> <remove name="X-Powered-By" /> </customHeaders> </httpProtocol> 再看看响应头,或用站长工具查看,完美隐藏。 参考资料: http://www.yn-s.com/news/Details/93
View Details1、指定表单提交方式和路径等
|
1 |
@using (Html.BeginForm("Index", "Home", FormMethod.Get, new { name = "nbform", id = "nbform" })) |
2、指定表单提交为数据方式
|
1 |
@using (Html.BeginForm("ImportExcel", "Stock", FormMethod.Post, new { enctype = "multipart/form-data" })) |
注意, 有时候要加{id=1}不然在点击超过第一页的索引时form后面会自动加上当前页的索引,如果此时再搜索则可能会出来“超出索引值”的错误提示 @using (Html.BeginForm("Index", null, new { id = 1 }, FormMethod.Get)) 3、下面的操作可以防止提交链接后面自带参数
|
1 |
@using (Html.BeginForm("AddDIYBillOfLading", "BillOfLading", new { id ="" }, FormMethod.Post, new { name = "myform", id = "myform" })) |
即,RouteValues的id="" from:http://www.cnblogs.com/firstcsharp/archive/2013/08/05/3238321.html
View DetailsJSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式。在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍,ASP.NET如何序列化和反序列化的处理,在序列化和反序列化对日期时间、集合、字典的处理。 一、JSON简介 JSON(JavaScript Object Notation,JavaScript对象表示法)是一种轻量级的数据交换格式。 JSON是“名值对”的集合。结构由大括号'{}’,中括号'[]’,逗号’,’,冒号’:’,双引号’“”’组成,包含的数据类型有Object,Number,Boolean,String,Array, NULL等。 JSON具有以下的形式: 对象(Object)是一个无序的“名值对”集合,一个对象以”{”开始,”}”结束。每个“名”后跟着一个”:”,多个“名值对”由逗号分隔。如:
|
1 |
var user={"name":"张三","gender":"男","birthday":"1980-8-8"} |
数组(Array)是值的有序集合,一个数组以“[”开始,以“]”结束,值之间使用“,”分隔。如:
|
1 |
var userlist=[{"user":{"name":"张三","gender":"男","birthday":"1980-8-8"}},{"user":{"name":"李四","gender":"男","birthday":"1985-5-8"}}]; |
字符串(String)是由双引号包围的任意数量的Unicode字符的集合,使用反斜线转义。 二、对JSON数据进行序列化和反序列化 可以使用DataContractJsonSerializer类将类型实例序列化为JSON字符串,并将JSON字符串反序列化为类型实例。DataContractJsonSerializer在System.Runtime.Serialization.Json命名空间下,.NET Framework 3.5包含在System.ServiceModel.Web.dll中,需要添加对其的引用;.NET Framework 4在System.Runtime.Serialization中。 利用DataContractJsonSerializer序列化和反序列化的代码:
|
1 |
1: using System; |
|
1 |
2: using System.Collections.Generic; |
|
1 |
3: using System.Linq; |
|
1 |
4: using System.Web; |
|
1 |
5: using System.Runtime.Serialization.Json; |
|
1 |
6: using System.IO; |
|
1 |
7: using System.Text; |
|
1 |
8: |
|
1 |
9: /// <summary> |
|
1 |
10: /// JSON序列化和反序列化辅助类 |
|
1 |
11: /// </summary> |
|
1 |
12: public class JsonHelper |
|
1 |
13: { |
|
1 |
14: /// <summary> |
|
1 |
15: /// JSON序列化 |
|
1 |
16: /// </summary> |
|
1 |
17: public static string JsonSerializer<T>(T t) |
|
1 |
18: { |
|
1 |
19: DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); |
|
1 |
20: MemoryStream ms = new MemoryStream(); |
|
1 |
21: ser.WriteObject(ms, t); |
|
1 |
22: string jsonString = Encoding.UTF8.GetString(ms.ToArray()); |
|
1 |
23: ms.Close(); |
|
1 |
24: return jsonString; |
|
1 |
25: } |
|
1 |
26: |
|
1 |
27: /// <summary> |
|
1 |
28: /// JSON反序列化 |
|
1 |
29: /// </summary> |
|
1 |
30: public static T JsonDeserialize<T>(string jsonString) |
|
1 |
31: { |
|
1 |
32: DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); |
|
1 |
33: MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); |
|
1 |
34: T obj = (T)ser.ReadObject(ms); |
|
1 |
35: return obj; |
|
1 |
36: } |
|
1 |
37: } |
序列化Demo: 简单对象Person:
|
1 |
1: public class Person |
|
1 |
2: { |
|
1 |
3: public string Name { get; set; } |
|
1 |
4: public int Age { get; set; } |
|
1 |
5: } |
序列化为JSON字符串:
|
1 |
1: protected void Page_Load(object sender, EventArgs e) |
|
1 |
2: { |
|
1 |
3: Person p = new Person(); |
|
1 |
4: p.Name = "张三"; |
|
1 |
5: p.Age = 28; |
|
1 |
6: |
|
1 |
7: string jsonString = JsonHelper.JsonSerializer<Person>(p); |
|
1 |
8: Response.Write(jsonString); |
|
1 |
9: } |
输出结果:
|
1 |
{"Age":28,"Name":"张三"} |
反序列化Demo:
|
1 |
1: protected void Page_Load(object sender, EventArgs e) |
|
1 |
2: { |
|
1 |
3: string jsonString = "{\"Age\":28,\"Name\":\"张三\"}"; |
|
1 |
4: Person p = JsonHelper.JsonDeserialize<Person>(jsonString); |
|
1 |
5: } |
运行结果: ASP.NET中的JSON序列化和反序列化还可以使用JavaScriptSerializer,在System.Web.Script.Serializatioin命名空间下,需引用System.Web.Extensions.dll.也可以使用JSON.NET. […]
View Detailsusing System; using System.Collections.Generic; using System.Text; namespace NET.MST.Fourth.StringByte { class StringByte { static void Main(string[] args) { String s = "我是字符串,I am string"; //字节数组转换到字符串 Byte[] utf8 = StringToByte(s, Encoding.UTF8); Byte[] gb2312 = StringToByte(s, Encoding.GetEncoding("GB2312")); Byte[] unicode = StringToByte(s, Encoding.Unicode); Console.WriteLine(utf8.Length); Console.WriteLine(gb2312.Length); Console.WriteLine(unicode.Length); //转换回字符串 Console.WriteLine(ByteToString(utf8, Encoding.UTF8)); Console.WriteLine(ByteToString(gb2312, Encoding.GetEncoding("GB2312"))); Console.WriteLine(ByteToString(unicode, Encoding.Unicode)); Console.Read(); } static Byte[] StringToByte(String s, Encoding encoding) { return encoding.GetBytes(s); } static String ByteToString(Byte[] b, Encoding encoding) { return encoding.GetString(b); } } } from:http://www.cnblogs.com/brainmao/archive/2011/05/29/2062385.html
View Details介绍 让用户从我们的网站上下载各种类型的文件是一个比较常用的功能,这篇文章就是告诉您如何创建一个.txt文件并让用户下载。 使用代码 虽然在示例里,我先创建了一个text文件,但是你不一定也要这么做,因为这个文件可能在你的网站里已经存在了。如果是这样的话,你只需要使用FileStream去读取它就可以了。 首先,我们将这个text文件读取到一个byte数组中,然后使用Response对象将文件写到客户端就可以了。 Response.AddHeader("Content-disposition", "attachment; filename=" + sGenName); Response.ContentType = "application/octet-stream"; Response.BinaryWrite(btFile); Response.End(); 这段代码是完成这个功能的主要代码。第一句在输出中添加了一个Header,告诉浏览器我们发送给它的是一个附件类型的文件。然后我们设置输出的ContentType是"application/octet-stream",即告诉浏览器要下载这个文件,而不是在浏览器中显示它。 下面是一个MIME类型的列表。 ".asf" = "video/x-ms-asf" ".avi" = "video/avi" ".doc" = "application/msword" ".zip" = "application/zip" ".xls" = "application/vnd.ms-excel" ".gif" = "image/gif" ".jpg"= "image/jpeg" ".wav" = "audio/wav" ".mp3" = "audio/mpeg3" ".mpg" "mpeg" = "video/mpeg" ".rtf" = "application/rtf" ".htm", "html" = "text/html" ".asp" = "text/asp" '所有其它的文件 = "application/octet-stream" 下面是一个完整的如何下载文本文件的示例代码 C# protected void Button1_Click(object sender, EventArgs e) { string sFileName = System.IO.Path.GetRandomFileName(); string sGenName = "Friendly.txt"; //YOu could omit these lines here as you may not want […]
View DetailsIIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法 IIS上部署MVC网站,打开后500错误:处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler” 解决方法如下: 以管理员运行下面的命令注册: 32位机器: C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i 64位机器: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i
View Details最近新换了系统还真是问题多多呀!! 系统更新补丁后打开 VS2012 ,新建C#项目的时候出现这个问题 VS2012 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDoc mentFactoryService 我勒个去,相当郁闷呀,赶紧百度,找到了下面两个解决方案: 方案一: 删除 kb2805222 .net4.5 framework 更新程序(控制面板->Windows Update->) 但是我这里没有这个更新 于是我采用第二套方案 方案二: 更新 (KB2781514) 下载网址:http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=36020 OK了!哈哈~~~~ from:http://www.cnblogs.com/csulennon/p/3709019.html
View Details在Sql Server2005中,如果将某字段定义成日期 时间 类型DateTime,那么在视图中会默认显示成年月日时分秒的方式(如 2013/8/6 13:37:33) 如果只想显示成年月日形式,不要时分秒,那么该怎么办呢? 第一种方法:先设置一个时间显示的模板,然后在需要显示时间的地方调用这个模板就行了。 1、在Share文件夹下,创建一个文件夹DisplayTemplates 2、在DisplayTemplates文件夹下,创建一个视图LongDateTime.cshtml 3、在视图LongDateTime.cshtml中输入代码
|
1 2 3 |
<span class="variable">@model</span> <span class="constant">System</span>.<span class="constant">DateTime</span> <span class="variable">@Model</span>.<span class="constant">ToLongDateString</span>() |
当然,后面那句也可以换成@Model.ToShortDateString()或其它日期格式。 4、在需要显示日期的地方,由原来的
|
1 |
<span class="at_rule">@Html.<span class="function">DisplayFor(modelItem => item.PostTime)</span></span> |
替换成
|
1 |
<span class="at_rule">@Html.<span class="function">DisplayFor(modelItem => item.PostTime,<span class="string">"</span><span class="string">LongDateTime"</span>)</span></span> |
这样就完成了时间格式的显示转换。由原来的显示方式(2013/8/6 13:37:33)显示成了(2013年8月6日) 第二种方法:model类上面添加DisplayFormat的attribute. 如:
|
1 2 3 4 5 6 7 |
[Display(Name = <span class="string">"</span><span class="string">发布时间:"</span>)] [DisplayFormat(DataFormatString = <span class="string">"</span><span class="string">{0:yyyy年MM月dd日}"</span>)] <span class="keyword">public</span> <span class="keyword">virtual</span> System.DateTime PostTime { <span class="keyword">get</span>; <span class="keyword">set</span>; } |
显示出来后,照样是2013年8月6日这种格式。 from:http://www.cnblogs.com/Rising/p/3722299.html
View DetailsASP.NET MVC3中的Model是自验证的,这是通过.NET4的System.ComponentModel.DataAnnotations命名空间完成的。 我们要做的只是给Model类的各属性加上对应的验证标记(Attributes)就可以让MVC3框架帮我们完成验证。我以MVC3项目模板自带的登录 做例子讲解Model的验证。 一、启用客户端验证: 客户端验证主要是为了提高用户体验,在网页不回刷的情况下完成验证。 第一步是要在web.config里启用客户端验证,这在MVC3自带的模板项目中已经有了: <add key="ClientValidationEnabled" value="true"/> <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 然后在被验证的View页面上要加入这样两个JavaScript,注意,他们是依赖于JQuery的: <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 验证消息的显示有两种,一种是ValidationSummary,它可以显示一份验证消息的汇总,包括从后台Action里返回的消息。 @Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") 另一种是Model中各属性对应HTML控件的验证消息: @Html.ValidationMessageFor(m => m.UserName) 二、在Model中加入验证标记 MVC3项目模板自带的登录模型类如下: public class LogOnModel { [Required] [Display(Name = "User name")] public string UserName { get; set; } [Required] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [Display(Name = "Remember me?")] public bool RememberMe { get; set; } } 对比普通的C#类,我们发现每个属性上都多了被方括号“[]”包围的标记。其中,[Required]是验证标记的一种,而[Display]、[DataType]则是为了显示对应的HTML控件,这不在本文讨论范围之内。 除了Required,我们还可以在Model中添加其他有用的验证标记。下面是个较完整的列表: Model类中可以添加的验证标记: 1. 必填字段 [Required] public string FirstName { […]
View Details