Response.Redirect原理及实现-创新互联

之前一直以为Response.Redirect (“default1.aspx”)的运行原理是这样的Response.Redirect 原理及实现

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、虚拟空间、营销软件、网站建设、达拉特网站维护、网站推广。

但经过断点测试发现不是,当程序执行到Response.Redirect (“default1.aspx”);时 下边会跳转到default1.aspx下的Page_Load()方法中。也就是说2,3根本没有运行,只有1跟4,浏览器在根据4返回的状态码来在前台地址栏显示出不同的url,说到这里了 咱们就反编译一把Response.Redirect看看里面的具体实现:

internalvoidRedirect(string url, bool endResponse, bool permanent)
{
    if (url == null)
    {
        thrownewArgumentNullException("url");
    }
    if (url.IndexOf('\n') >= 0)
    {
        thrownewArgumentException(SR.GetString("Cannot_redirect_to_newline"));
    }
    if (this._headersWritten)
    {
        thrownewHttpException(SR.GetString("Cannot_redirect_after_headers_sent"));
    }
    Pagepage = this._context.HandlerasPage;
    if ((page != null) && page.IsCallback)
    {
        thrownewApplicationException(SR.GetString("Redirect_not_allowed_in_callback"));
    }
    url = this.ApplyRedirectQueryStringIfRequired(url);
    url = this.ApplyAppPathModifier(url);
    url = this.ConvertToFullyQualifiedRedirectUrlIfRequired(url);
    url = this.UrlEncodeRedirect(url);
    this.Clear();
    if (((page != null) && page.IsPostBack) && (page.SmartNavigation && (this.Request["__smartNavPostBack"] == "true")))
    {
        this.Write("");
        this.Write("");
    }
    else
    {
        this.StatusCode = permanent ? 0x12d : 0x12e;
        this.RedirectLocation = url;
        if (UriUtil.IsSafeScheme(url))
        {
            url = HttpUtility.HtmlAttributeEncode(url);
        }
        else
        {
            url = HttpUtility.HtmlAttributeEncode(HttpUtility.UrlEncode(url));
        }
        this.Write("Object moved\r\n");
        this.Write("

Object moved to here.

\r\n"); this.Write("\r\n"); } this._isRequestBeingRedirected = true; EventHandlerredirecting = Redirecting; if (redirecting != null) { redirecting(this, EventArgs.Empty); } if (endResponse) { this.End(); } }

其中由37,38行的

this.StatusCode = permanent ? 0x12d : 0x12e;

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:Response.Redirect原理及实现-创新互联
网站网址:http://lszwz.com/article/csihii.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款