简体   繁体   English

MVC5本地化与路线文化-拥有区域的问题

[英]MVC5 Localization with culture in route - issues with having an Area

I've had a look at the following blog post, and implemented the code as has been laid out: 我看了以下博客文章,并按照已计划的方式实现了代码:

ASP.NET MVC 5 Internationalization ASP.NET MVC 5国际化

Without areas in my MVC5 application, it works really well. 我的MVC5应用程序中没有区域,它确实运行良好。 A user navigates to http://localhost and with the default language of my browser set to Italian (it) I end up with http://localhost/it . 用户导航到http://localhost并且浏览器的默认语言设置为Italian(it),我最终使用http://localhost/it Perfect. 完善。

With an area registered in my application, everything breaks down. 我的应用程序注册的区域,一切都打破了。 A user navigating to http://localhost as per above ends up trying to hit http://localhost/it/myarea/Home , which doesn't exist. 按照上述方法导航到http://localhost用户最终尝试点击http://localhost/it/myarea/Home ,该地址不存在。

As per the referenced blog post, here's what I have: 根据引用的博客文章,这是我所拥有的:

My route configuration 我的路线配置

namespace MyWebsite
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //routing to include culture
            routes.MapRoute(
                name: "DefaultWithCulture",
                url: "{culture}/{controller}/{action}/{id}",
                defaults: new { culture = string.Empty, controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

My area registration 我的地区注册

namespace MyWebsite.Areas.WindowShop
{
    public class MyAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "myarea";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "myarea_default",
                "{culture}/myarea/{controller}/{action}/{id}",
                new { action = "Index", culture = string.Empty, id = UrlParameter.Optional }
            );
        }
    }
}

I have a BaseController that should inject the culture of the browser if it hasn't already been supplied in the route. 我有一个BaseController ,如果路由中尚未提供浏览器的BaseController ,则应注入该区域性。

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

            if (string.IsNullOrEmpty(cultureName))
                cultureName = Request.UserLanguages != null && Request.UserLanguages.Length > 0 ? Request.UserLanguages[0] : null; // obtain it from HTTP header AcceptLanguages

            // some validation
            cultureName = CultureHelper.GetImplementedCulture(cultureName);

            if (RouteData.Values["culture"] as string != cultureName)
            {
                RouteData.Values["culture"] = cultureName.ToLowerInvariant();

                Response.RedirectToRoute(RouteData.Values);
            }

            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

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

I suspect that something is getting screwed up with the routing, because we seem (with the Area enabled) to be attempting to hit a HomeController within the area, but no controller of that name exists within that area (there is a root HomeController ). 我怀疑的东西是越来越搞砸了与路由,因为我们似乎(启用的区域),然后再尝试打一个HomeController区域内,但没有这个名字的控制器,区域内存 (有根HomeController )。

I really don't understand why, when the area is in play, we end up trying to redirect to a controller that doesn't exist. 我真的不明白为什么,当该区域处于活动状态时,我们最终试图重定向到不存在的控制器。

I'm looking to get this working, as I'd like to keep the areas, and I am also trying to avoid using cookies (I'd rather have the culture in the URL). 我想使此工作正常进行,因为我想保留这些区域,并且我还尝试避免使用Cookie(我希望URL中包含区域性)。

After struggling with the same Problem for some time i found an answer working for me. 在同一个问题上挣扎了一段时间后,我找到了一个对我有用的答案。 In your Global.asax just make sure to register the Routes before registering the Areas: 在您的Global.asax中,只需确保注册区域之前先注册路线:

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

I had exaclty the same problem following this internationalization post and solved it by adding area = AreaName as a parameter of the default area route and area = "" as a parameter of the default route: 在此国际化发布之后,我遇到了同样的问题,并通过添加area = AreaName作为默认区域路由的参数和area =“”作为默认路由的参数来解决它:

Area route configuration: 区域路由配置:

    public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            "myarea_default",
            "{culture}/myarea/{controller}/{action}/{id}",
            new { action = "Index", culture = string.Empty, area = AreaName, id = UrlParameter.Optional }
        );
    }

Route configuration: 路由配置:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //routing to include culture
        routes.MapRoute(
            name: "DefaultWithCulture",
            url: "{culture}/{controller}/{action}/{id}",
            defaults: new { culture = string.Empty, controller = "Home", action = "Index", area = "", id = UrlParameter.Optional }
        );
    }

If you have the same controller/action in distinct areas, you must add the namespaces too. 如果在不同区域中具有相同的控制器/动作,则也必须添加名称空间。

Hope it works for you too. 希望它也对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM