2015-12-26 96 views
1

有人可以解釋爲什麼屬性IsRequestBeingRedirected在我的代碼中始終爲false,以及如何正確重定向以獲取真實值?Response.IsRequestBeingRedirected始終爲假

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
    base.OnActionExecuting(filterContext); 
    if (filterContext.RequestContext.HttpContext.Response.IsRequestBeingRedirected) 
    { 
     //some another logic 
    } 
    //some code  
    filterContext.RequestContext.HttpContext.Response.Redirect(filterContext.RequestContext.HttpContext.Request.Url.AbsolutePath); 
} 

回答

2

IsRequestBeingRedirected屬性時調用Response.Redirect設置爲true 。這是通過.net反射源代碼:

public void Redirect(string url) 
{ 
    this.Redirect(url, true, false); 
} 

public void Redirect(string url, bool endResponse) 
{ 
    this.Redirect(url, endResponse, false); 
} 

internal void Redirect(string url, bool endResponse, bool permanent) 
{ 
    if (url == null) 
    { 
     throw new ArgumentNullException("url"); 
    } 
    if (url.IndexOf('\n') >= 0) 
    { 
     throw new ArgumentException(System.Web.SR.GetString("Cannot_redirect_to_newline")); 
    } 
    if (this._headersWritten) 
    { 
     throw new HttpException(System.Web.SR.GetString("Cannot_redirect_after_headers_sent")); 
    } 
    Page page = this._context.Handler as Page; 
    if ((page != null) && page.IsCallback) 
    { 
     throw new ApplicationException(System.Web.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("<h2>Object moved to <a href=\"" + url + "\">here</a>.</h2>\r\n"); 
     this.Write("</body></html>\r\n"); 
    } 

    // IsRequestBeingRedirected is set here 
    this._isRequestBeingRedirected = true; 
    EventHandler redirecting = Redirecting; 
    if (redirecting != null) 
    { 
     redirecting(this, EventArgs.Empty); 
    } 
    if (endResponse) 
    { 
     this.End(); 
    } 
} 

所以,你有你打電話Response.Redirect後這是真的來檢查值。

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
    base.OnActionExecuting(filterContext); 

    //some code  
    filterContext.RequestContext.HttpContext.Response.Redirect(filterContext.RequestContext.HttpContext.Request.Url.AbsolutePath); 

    if (filterContext.RequestContext.HttpContext.Response.IsRequestBeingRedirected) 
    { 
     //some another logic 
    } 
} 

當然,你的邏輯不是很清楚。你已經知道你正在重定向,因爲你打電話給Response.Redirect,所以在這種情況下檢查這個屬性似乎是多餘的。

+0

我只是試圖編寫邏輯來檢測瀏覽器在運行時是否禁用了Cookie。我不知道如何通過另一種方式實現此目的。 – user3818229

+0

如果您在Google上搜索,則已有許多cookie檢查解決方案。 [這一個](http://stackoverflow.com/questions/3716710/how-to-check-cookie-enabled-for-the-browser-or-not#4149481)看起來很有前途。 – NightOwl888