1)、如何获取对象:html
在WebForm或类库(包括MVC)项目中,经过Current静态属性,就可以得到HttpContext的对象: HttpContext context = HttpContext.Current;web
若是是在Asp.net MVC的Controller中,经过this.HttpContext;就能获取到HttpContextBase对象:HttpContextBase context = this.HttpContext;缓存
若是是在MVC视图中能够这样获得:@Html.ViewContext.HttpContext服务器
2)、貌似HttpContext中有不少对象属性在Page中也有,例如Request,Response,Cache,Session等等,那它们是什么关系呢?网络
是同一个对象。app
//获取上一次异常异步
HttpContext.Current.Server.GetLastError();ide
//清空异常post
HttpContext.Current.Server.ClearError();网站
HttpContext.Current在异步线程中是获取不到的,为null
获取当前网站、应用程序根目录:AppDomain.CurrentDomain.BaseDirectory
2、Asp.Net Web Form 页面
1) 页面中多个表单元素名称相同时,传入的值为 逗号分隔:如 有两个<input type="text" name="age" /> 元素 一个值是10 一个值是20,传入的时,age:10,20
2) 按钮事件 当前页面的请求地址不会改变 , 如,请求地址为: http://localhost:55519/Test.aspx?no=qq 在这个页面点击服务器按钮时 请求地址不会改变 ,即查询字符串?no=qq 信息不会丢失。
原理:表单控件的默认action 为当前请求地址。
<form method="post" action="Test.aspx?no=qq" id="form1"> <input type="submit" name="btnTest" value="ces" id="btnTest"><form>
3)服务器控件有视图状态 在提交表单后 值仍存在 不用从新输入 , html 客户端控件则提交后数据清空
web.config 中设置单个文件上传大小:maxRequestLength 默认值 4096 KB (4 MB)
<httpRuntime maxRequestLength="4096" appRequestQueueLimit="60" executionTimeout="60"/>
3、部署服务器
一、应用程序池--》高级设置--》启动32位应用程序 true--》托管管道模式选择Classic FrameWork选择4.0版 32位 经典模式(Class)
二、网站:功能视图:处理程序映射:Copy 4.0版32位(HttpRemotingHandlerFactory-rem-ISAPI-4.0_32bit的可执行文件) 右击添加 通配符脚本映射:名称随便起 可执行文件用上一步
三、若是有上传文件功能 添加IIS Pool权限:
4、自定义错误页面:
方法一:web.config
configuration> <system.web> <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly|On|Off"> <error statusCode="500" redirect="InternalError.htm"/>
<error statusCode="404" redirect="404.htm"/>
</customErrors>
</system.web>
</configuration>
方法二:自定义Page类
4、
//获取最后的异常。
//获取前一个异常
Exception ex = HttpContext.Current.Server.GetLastError();
//清除前一个异常。
HttpContext.Current.Server.ClearError();
5、
在含有验证控件的页面 想让某个服务器按钮 免验证提交事件 可设置其 属性 CausesValidation="False"
6、
//ASP.NET后台页面跳转
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>if(confirm('保存成功!是否继续添加?')){location.href='ProductonAdd.aspx'}else{location.href='ProductonList.aspx'}</script>");
//后台弹出肯定框
ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('请正确输入!');</script>");
//ASP.NET后台页面跳转
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('数据添加成功!');{location.href='ProductonList.aspx'}</script>");
或
Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>window.location.href='AdminMain.aspx';</script>");
//后台弹出文本框
ScriptManager.RegisterStartupScript(Page, typeof(string), "popUp", "window.open('rptView.aspx','打印预览','toolbar=no,location=no,scrollbars=yes,top=200px,left=200px,width=904px,height=650px')", true);
清除DNS缓存
遇到网络异常,多是DNS缓存的问题,清理一下便可。
①开始→运行→输入:CMD 按回车键,打开命令提示符窗口。
②再输入: ipconfig /flushdns 回车执行命令,重建本地DNS缓存。
含有表单验证控件的页面在验证未经过时按钮事件是不没法提交的