2014-10-16 16 views
0

我遇到的情況,我有我的所有航線前面帶有國家代碼,例如http://www.example.com/US/Home - 國家代碼用於在會話中存儲特定於國家的信息。這是除未認證的請求(如輪詢AJAX請求火災後的會話已過期,或者用戶離開他們的瀏覽器打開時和部署後的很長一段時間),此時系統會嘗試重定向到所有工作正常/Account/Login,而不是國家特定的網址,例如/US/Account/Login。因爲/Account/Login是不是一個有效的路由系統將引發The controller for path '/Account/Login' could not be found or it does not implement IController.例外。如何獲得重定向到/帳號/登錄特定路由配置工作

一等獎將能夠在重定向到一個特定的國家如/US/Account/Login,但這是不可能的,我可以使用/Account/Login。如果我在一個路由添加用於/Account/Login它獲取由DefaultCountryCode路線取代(如果在路由配置下面放置),或者它取代了DefaultCountryCode路線(如果在路由配置下面放置)。

路由配置

// if placed here this supersedes DefaultCountryCode route 
//routes.MapRoute("Account", "/Account/Login"); 

// e.g. http://www.example.com/US/Home 
routes.MapRoute("DefaultCountryCode", "{countrycode}/{controller}/{action}/{id}", 
    new 
    { 
     controller = "Home", 
     action = "Index", 
     id = UrlParameter.Optional 
    }); 

// if placed here this gets superseded by DefaultCountryCode route 
//routes.MapRoute("Account", "/Account/Login"); 
+0

你有沒有在你的LOGINPATH的Startup.Auth.cs文件哪些設置? – DavidG 2014-10-16 20:04:27

回答

0

你有沒有看着attribute routing呢?您可以創建你的控制器,並有兩個動作 -

一個將被調用時,你有「美國/帳號/登錄/(編號)」,另一個可以有屬性「帳戶/登錄。」

1

創建自定義授權屬性,並將其添加到您的控制器或全局註冊:

public class CustomAuthorizeAttribute : AuthorizeAttribute 
{ 
    public override void OnAuthorization(AuthorizationContext filterContext) 
    { 
     base.OnAuthorization(filterContext); 
     if (filterContext.Cancel && filterContext.Result is HttpUnauthorizedResult) 
     { 
     filterContext.Result = new RedirectToRouteResult(
      new RouteValueDictionary { 
      { "controller", "Account" }, 
      { "action", "Login" }, 
      { "countrycode", /* extract it from: filterContext.HttpContext.Request.RawUrl or somewhere from context */ } 
     }); 
     } 
    } 
} 
相關問題