您好,登錄后才能下訂單哦!
之前一直以為Response.Redirect (“default1.aspx”)的運行原理是這樣的
但經過斷點測試發現不是,當程序執行到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>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。