2011-08-16 37 views
22

在我的web應用程序中,我正在驗證來自glabal.asax的url。我想驗證網址,如果需要的話需要重定向到一個動作。我正在使用Application_BeginRequest來捕獲請求事件。重定向到global.asax的動作 - mvc

protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     // If the product is not registered then 
     // redirect the user to product registraion page. 
     if (Application[ApplicationVarInfo.ProductNotRegistered] != null) 
     { 
      //HOW TO REDIRECT TO ACTION (action=register,controller=product) 
     } 
    } 

或者有沒有其他的方法來驗證每一個URL同時獲得MVC中的請求,並根據需要

+0

http://stackoverflow.com/questions/580728/redirecting-from-global-asax-in-medium-trust – Baz1nga

回答

22

使用下面的代碼重定向

Response.RedirectToRoute("Default"); 

「默認」 爲路徑名。如果您想重定向到任何操作,只需創建一個路線並使用該路線名稱即可。

+3

這將創建循環 – Fourat

+0

@null指針,請你能接受一個可能的複製其他答案有效嗎? –

+0

以@Fourat指出的方式創建循環 – invertigo

-2

重定向到一個動作你可以利用這個嘗試:

Context.Response.Redirect() ;

確定。

5

試試這個:

HttpContext.Current.Response.Redirect("..."); 
18

以上所有將無法正常工作,您將會執行方法Application_BeginRequest的循環。

您需要使用

HttpContext.Current.RewritePath("Home/About"); 
+0

這是正確的答案。 – pimbrouwers

0
Response.RedirectToRoute(
           new RouteValueDictionary { 
            { "Controller", "Home" }, 
            { "Action", "TimeoutRedirect" }} ); 
+0

這在'Application_BeginRequest'中不起作用。 – AgentFire

+0

這將導致另一個請求;這會導致另一個請求。這導致另一個請求。這創造了另一個。直到完全錯誤。 –

2

我不喜歡這樣寫道:

 HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context); 

     RouteData routeData = new RouteData(); 
     routeData.Values.Add("controller", "Home"); 
     routeData.Values.Add("action", "FirstVisit"); 

     IController controller = new HomeController(); 

     RequestContext requestContext = new RequestContext(contextWrapper, routeData); 

     controller.Execute(requestContext); 
     Response.End(); 

這樣你包傳入的請求上下文並將其重定向到其他地方,而不會重定向客戶端。所以重定向不會在global.asax中觸發另一個BeginRequest。

7

除了已經提到的方式。另一種方法是使用URLHelper這是我在一個場景中使用一次錯誤happend和用戶應重定向到登錄頁面:

public void Application_PostAuthenticateRequest(object sender, EventArgs e){ 
    try{ 
     if(!Request.IsAuthenticated){ 
      throw new InvalidCredentialException("The user is not authenticated."); 
     } 
    } catch(InvalidCredentialException e){ 
     var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); 
     Response.Redirect(urlHelper.Action("Login", "Account")); 
    } 
} 
+1

這會導致另一個請求。這反過來使另一個,。這是另一個。等你進入application_begin請求函數時。 –

+1

@RichardBarker此代碼不在application_begin請求!!!!我把它放在Application_PostAuthenticateRequest中的一個catch塊中cas錯誤發生了 –

+0

我的道歉。問題顯示BeginRequest。我可以建議你更新你的答案以表明並解釋你爲什麼這麼做嗎? –

0

我有一箇舊的Web窗體應用程序,我不得不轉換爲MVC 5的要求之一正在支持可能的{old_form} .aspx鏈接。在Global.asax Application_BeginRequest中,我設置了一個switch語句來處理舊頁面以重定向到新頁面,並避免可能不希望的循環到請求原始URL中的「.aspx」的主/默認路由檢查。

protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     OldPageToNewPageRoutes(); 
    } 

    /// <summary> 
    /// Provide redirects to new view in case someone has outdated link to .aspx pages 
    /// </summary> 
    private void OldPageToNewPageRoutes() 
    { 
     // Ignore if not Web Form: 
     if (!Request.RawUrl.ToLower().Contains(".aspx")) 
      return; 

     // Clean up any ending slasshes to get to the old web forms file name in switch's last index of "/": 
     var removeTrailingSlash = VirtualPathUtility.RemoveTrailingSlash(Request.RawUrl); 
     var sFullPath = !string.IsNullOrEmpty(removeTrailingSlash) 
      ? removeTrailingSlash.ToLower() 
      : Request.RawUrl.ToLower(); 
     var sSlashPath = sFullPath; 

     switch (sSlashPath.Split(Convert.ToChar("/")).Last().ToLower()) 
     { 
      case "default.aspx": 
       Response.RedirectToRoute(
        new RouteValueDictionary 
        { 
         {"Controller", "Home"}, 
         {"Action", "Index"} 
        }); 
       break; 
      default: 
       // Redirect to 404: 
       Response.RedirectToRoute(
        new RouteValueDictionary 
        { 
         {"Controller", "Error"}, 
         {"Action", "NotFound"} 
        }); 
       break; 

     } 
    }