首页 > 解决方案 > 锚标记助手不重用当前路由值

问题描述

目前我正在使用 ASP.NET Core 2.2 MVC 来构建我的应用程序,具有模板的常规路线:

routes.MapRoute(
    name: "default",
    template: "{culture:required}/{area:slugify:exists}/{controller:slugify}/{action:slugify}",
    defaults: new
    {
        area = "Home",
        controller = "Home",
        action = "Index"
    });

/Areas/Home/Views/Home/Index.cshtml中,我尝试编写以下内容并期望会生成类似“ http://localhost/en-US/Home/Home/AboutUs ”的内容,但它不起作用:

<a 
   asp-area="Home" 
   asp-controller="Home" 
   asp-action="AboutUs">
    About Us
</a>

相反,明确指定文化有效:

<a asp-area="Home" 
   asp-controller="Home" 
   asp-action="AboutUs" 
   asp-route-culture="en-US">
    About Us
</a>

但是,由于我的网站包含大量链接,我不想用CultureInfo.CurrentCulture.Name. 如果没有明确指定,是否有一种聪明的方法可以强制它继承当前的路由值asp-route-culture

先感谢您。

标签: asp.net-coreasp.net-core-mvcasp.net-core-2.2asp.net-core-routing

解决方案


routes.MapRoute(
    name: "us_english_products",
    template: "en-US/Products/{id?}",
    defaults: new { controller = "Products", action = "Details" },
    dataTokens: new { locale = "en-US" });

我想你正在寻找这个。它位于下面的文档中

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-2.2#differences-from-earlier-versions-of-routing

因此,您不必对每个链接上的路线进行硬编码,但您可以设置路线以便将其考虑在内。看来您可以随心所欲地命名,locale所以如果您想命名culture就可以。


推荐阅读