之前一直以為Response.Redirect (“default1.aspx”)的運(yùn)行原理是這樣的
創(chuàng)新互聯(lián)主要業(yè)務(wù)有網(wǎng)站營(yíng)銷策劃、網(wǎng)站制作、成都網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、微信小程序開(kāi)發(fā)、HTML5建站、程序開(kāi)發(fā)等業(yè)務(wù)。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當(dāng)客戶,還把客戶視為我們的合作伙伴,在開(kāi)展業(yè)務(wù)的過(guò)程中,公司還積累了豐富的行業(yè)經(jīng)驗(yàn)、成都全網(wǎng)營(yíng)銷推廣資源和合作伙伴關(guān)系資源,并逐漸建立起規(guī)范的客戶服務(wù)和保障體系。
但經(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(""); 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;
可以看出狀態(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(""); context.Response.StatusCode = 200; } } } }
然后再配置一下web.config