网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Response.Redirect原理及实现

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

创新互联主要业务有网站营销策划、网站制作、成都网站制作、微信公众号开发、微信小程序开发HTML5建站、程序开发等业务。一次合作终身朋友,是我们奉行的宗旨;我们不仅仅把客户当客户,还把客户视为我们的合作伙伴,在开展业务的过程中,公司还积累了丰富的行业经验、成都全网营销推广资源和合作伙伴关系资源,并逐渐建立起规范的客户服务和保障体系。 

但经过断点测试发现不是,当程序执行到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;
this.RedirectLocation = url;

可以看出状态码和跳转地址是在这里赋值的,我邪恶的感觉到扩展IhttpModule可以拦截http状态码,使用js代码段来实现http页面的跳转,代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace ClassLibrary2
{
    public class Class1 : System.Web.IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.PreSendRequestHeaders += new EventHandler(application_PreSendRequestHeaders);
        }
        private void application_PreSendRequestHeaders(object sender, EventArgs e)
        {
            HttpApplication Application = (HttpApplication) sender;
            HttpContext context = Application.Context;
            if (context.Response.StatusCode == 302)
            {
                context.Response.Write("");
                context.Response.StatusCode = 200;
            }
        }
    }
}
  

然后再配置一下web.config


  
  
  
  
  
  
  
  
  

网站栏目:Response.Redirect原理及实现
本文来源:http://bjjierui.cn/article/ghcgpj.html

其他资讯