2016-09-07 25 views
9

Tôi đang cố gắng thiết lập BaseController để xử lý văn hóa như một phần của url (dựa trên ASP.NET MVC 5 Internationalization). Việc triển khai của tôi hoạt động đúng cách miễn là tôi vô hiệu hóa đăng ký Khu vực của tôi.Response.RedirectToRoute (RouteData.Values) chuyển hướng đến bộ điều khiển Area

Khi một trong các khu vực của tôi được đăng ký, nếu tôi cố nhập văn bản sai/không được hỗ trợ (http://localhost:52639/zz/), tôi gặp lỗi 404 với URL yêu cầu: http://localhost:52639/fr/Test/Post.

Tôi đã kiểm tra tuyến đường của mình được đăng ký đúng cách.

Nếu tôi làm như vậy trong khi vô hiệu hóa đăng ký Vùng, bộ điều khiển cơ sở và định tuyến hoạt động chính xác nếu tôi nhập URL sau: http://localhost:52639/zz/ Tôi được chuyển hướng đến http://localhost:52639/fr/ (văn bản mặc định).

Đó là những tuyến đường của tôi:

public static void RegisterRoutes(RouteCollection routes) 
    { 
     var namespaces = new[]{typeof(PostController).Namespace}; 

     routes.IgnoreRoute("favicon.ico"); 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute("PostToHack", "{culture}/Post/{idAndSlug}", new { culture = "", Controller = "Post", Action = "Show" }, namespaces); 
     routes.MapRoute("Post", "{culture}/Post/{id}-{slug}", new { culture = "", Controller = "Post", Action = "Show" }, namespaces); 

     routes.MapRoute("TagToHack", "{culture}/Tag/{idAndSlug}", new { culture = "", Controller = "Post", Action = "Tag" }, namespaces); 
     routes.MapRoute("Tag", "{culture}/Tag/{id}-{slug}", new { culture = "", Controller = "Post", Action = "Tag" }, namespaces); 

     routes.MapRoute("Logout", "{culture}/Logout", new { culture = "", Controller = "Authentication", Action = "Logout" }, namespaces); 
     routes.MapRoute("Login", "{culture}/Login", new { culture = "", Controller = "Authentication", Action = "Login" }, namespaces); 

     //Error routes 
     routes.MapRoute("Error404", "{culture}/errors/404", new { culture = "", Controller = "Errors", Action = "NotFound" }, namespaces); 
     routes.MapRoute("Error500", "{culture}/errors/500", new { culture = "", Controller = "Errors", Action = "Error" }, namespaces); 

     routes.MapRoute("Home", "{culture}", new { culture = "", Controller = "Post", Action = "Index"},namespaces); 

     //Never to be called by user which is why it comes after MapRoute Home so it is always overwritten by it 
     routes.MapRoute("Sidebar", "{culture}", new { culture = "", Controller = "Layout", Action = "Sidebar"},namespaces);//This is a "child-only" controller 
     routes.MapRoute("NavigationBar", "{culture}", new { culture = "", Controller = "Layout", Action = "NavigationBar"},namespaces);//This is a "child-only" controller 

Diện tích Route

public override void RegisterArea(AreaRegistrationContext context) 
    { 
     var namespaces = new[] { typeof(PostsController).Namespace }; 

     context.MapRoute(
      "admin_default", 
      "{culture}/admin/{controller}/{action}/{id}", 
      new { culture = "", action = "Index", id = UrlParameter.Optional }, namespaces 
      ); 
    } 

cơ sở điều khiển:

public class BaseController : Controller 
{ 
    protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state) 
    { 
     var cultureName = RouteData.Values["culture"] as string; 

     // Attempt to read the culture cookie from Request 
     if (cultureName == null) 
      cultureName = (Request.UserLanguages != null) && (Request.UserLanguages.Length > 0) 
       ? Request.UserLanguages[0] 
       : null; // obtain it from HTTP header AcceptLanguages 

     // Validate culture name 
     cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe 


     if (RouteData.Values["culture"] as string != cultureName) 
     { 
      // Force a valid culture in the URL 
      RouteData.Values["culture"] = cultureName.ToLowerInvariant(); // lower case too 

      // Redirect user 
      Response.RedirectToRoute(RouteData.Values); 
     } 

     // Modify current thread's cultures    
     Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName); 
     Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; 

     return base.BeginExecuteCore(callback, state); 
    } 
} 

Trả lời

0

Sau khi một số mo đào lại tôi đã tìm thấy một giải pháp phù hợp với tôi. Vấn đề của tôi đến từ thứ tự mà tôi đã đăng ký tuyến đường của mình. Tôi đã đăng ký các tuyến đường khu vực Mỹ đầu tiên:

protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
    } 

tôi đảo ngược trình tự, chắc chắn rằng tôi đã chỉ đăng ký các tuyến đường khu vực Mỹ sau:

protected void Application_Start() 
    { 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     AreaRegistration.RegisterAllAreas();    
    } 
Các vấn đề liên quan