亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Response.Redirect 原理及實現

發布時間:2020-08-04 11:00:16 來源:網絡 閱讀:938 作者:tongling_zzu 欄目:編程語言

之前一直以為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("<BODY><ASP_SMARTNAV_RDIR url=\"");
        this.Write(HttpUtility.HtmlEncode(url));
        this.Write("\"></ASP_SMARTNAV_RDIR>");
        this.Write("</BODY>");
    }
    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("<html><head><title>Object moved</title></head><body>\r\n");
        this.Write("<h3>Object moved to <a href=\"" + url + "\">here</a>.</h3>\r\n");
        this.Write("</body></html>\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("<script>window.open(’" + context.Response.RedirectLocation + "’)</script>");
                context.Response.StatusCode = 200;
            }
        }
    }
}
  

然后再配置一下web.config

<?xml version="1.0"?>
  <configuration>
  <system.web>
  <compilation debug="true"/>
  <authentication mode="Windows"/>
  <httpModules>
  <add name="MyModule" type="ClassLibrary2.Class1,ClassLibrary2"></add>
  </httpModules>
  </system.web>
  </configuration>


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

桃园县| 宝山区| 疏附县| 濮阳市| 亚东县| 赤壁市| 习水县| 朝阳县| 梅州市| 雷波县| 台中县| 灯塔市| 太仓市| 泉州市| 福鼎市| 平阴县| 鱼台县| 清涧县| 雅安市| 宣汉县| 兴城市| 云林县| 邹平县| 静海县| 蓬溪县| 哈尔滨市| 黄骅市| 岑巩县| 民县| 高雄市| 衢州市| 石嘴山市| 来安县| 汾西县| 平远县| 革吉县| 赣榆县| 百色市| 永州市| 稻城县| 湾仔区|