• ASP.NET基础系列


    一、HttpContext概述

    1)、如何获取对象:

     在WebForm或类库(包括MVC)项目中,通过Current静态属性,就能够获得HttpContext的对象: HttpContext context = HttpContext.Current;

     如果是在Asp.net MVC的Controller中,通过this.HttpContext;就能获取到HttpContextBase对象:HttpContextBase context = this.HttpContext;

    如果是在MVC视图中可以这样得到:@Html.ViewContext.HttpContext

    2)、貌似HttpContext中有很多对象属性在Page中也有,例如Request,Response,Cache,Session等等,那它们是什么关系呢?

    是同一个对象。

    //获取上一次异常

    HttpContext.Current.Server.GetLastError();

    //清空异常

    HttpContext.Current.Server.ClearError();

     HttpContext.Current在异步线程中是获取不到的,为null

    获取当前网站、应用程序根目录:AppDomain.CurrentDomain.BaseDirectory

    二、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"/>

    三、部署服务器

    1、应用程序池--》高级设置--》启动32位应用程序 true--》托管管道模式选择Classic  FrameWork选择4.0版 32位 经典模式(Class) 

    2、网站:功能视图:处理程序映射:Copy 4.0版32位(HttpRemotingHandlerFactory-rem-ISAPI-4.0_32bit的可执行文件) 右击添加 通配符脚本映射:名称随便起 可执行文件用上一步
    3、如果有上传文件功能 添加IIS Pool权限:

    四、自定义错误页面:

    方法一: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类

    自定义MyPage类继承与System.Web.UI.Page类,重写该类的OnError事件
    protected override void OnError(EventArgs e)
    {
          Response.Redirect("app1.aspx");
    }

     四、

    //获取最后的异常。

    //获取前一个异常
    Exception ex = HttpContext.Current.Server.GetLastError();

    //清除前一个异常。
    HttpContext.Current.Server.ClearError();

    五、

    在含有验证控件的页面 想让某个服务器按钮 免验证提交事件  可设置其 属性 CausesValidation="False"

     六、

     //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缓存。

    含有表单验证控件的页面在验证未通过时按钮事件是不无法提交的

     
  • 相关阅读:
    HDU 1150 Machine Schedule(二分匹配最小点覆盖)
    CodeForces 748F Santa Clauses and a Soccer Championship
    CodeForces 748E Santa Claus and Tangerines(二分)
    CodeForces 748D Santa Claus and a Palindrome (贪心+构造)
    POJ 3657 Haybale Guessing(二分+并查集)
    【JZOJ5773】简单数学题【数论,数学】
    【洛谷P4085】Haybale Feast【分块】
    【洛谷P4085】Haybale Feast【分块】
    【洛谷P4085】Haybale Feast【分块】
    【洛谷P4212】外太空旅行【随机】【贪心】
  • 原文地址:https://www.cnblogs.com/lxf1117/p/4478311.html
Copyright © 2020-2023  润新知