您好,登錄后才能下訂單哦!
之前一直以為Response.Redirect (“default1.aspx”)的運(yùn)行原理是這樣的
但經(jīng)過(guò)斷點(diǎn)測(cè)試發(fā)現(xiàn)不是,當(dāng)程序執(zhí)行到Response.Redirect (“default1.aspx”);時(shí) 下邊會(huì)跳轉(zhuǎn)到default1.aspx下的Page_Load()方法中。也就是說(shuō)2,3根本沒(méi)有運(yùn)行,只有1跟4,瀏覽器在根據(jù)4返回的狀態(tài)碼來(lái)在前臺(tái)地址欄顯示出不同的url,說(shuō)到這里了 咱們就反編譯一把Response.Redirect看看里面的具體實(shí)現(xiàn):
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;
可以看出狀態(tài)碼和跳轉(zhuǎn)地址是在這里賦值的,我邪惡的感覺(jué)到擴(kuò)展IhttpModule可以攔截http狀態(tài)碼,使用js代碼段來(lái)實(shí)現(xiàn)http頁(yè)面的跳轉(zhuǎn),代碼如下:
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>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。