Asp.net-Mvc

從 global.asax 中的 Application_BeginRequest 重定向到一個動作

  • August 16, 2011

在我的網路應用程序中,我正在驗證來自 glabal.asax 的 url。我想驗證 url,如果需要,需要重定向到一個動作。我正在使用 Application_BeginRequest 來擷取請求事件。

 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)
        }
    }

或者是否有任何其他方法可以在 mvc 中獲取請求並在需要時重定向到操作時驗證每個 url

使用以下程式碼進行重定向

  Response.RedirectToRoute("Default");

“預設”是路線名稱。如果您想重定向到任何操作,只需創建一個路由並使用該路由名稱。

以上所有都行不通,您將處於執行方法的循環中Application_BeginRequest

你需要使用

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

引用自:https://stackoverflow.com/questions/7076901