asp.net-core - 锚标记助手不重用当前路由值
问题描述
目前我正在使用 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
?
先感谢您。
解决方案
routes.MapRoute(
name: "us_english_products",
template: "en-US/Products/{id?}",
defaults: new { controller = "Products", action = "Details" },
dataTokens: new { locale = "en-US" });
我想你正在寻找这个。它位于下面的文档中
因此,您不必对每个链接上的路线进行硬编码,但您可以设置路线以便将其考虑在内。看来您可以随心所欲地命名,locale
所以如果您想命名culture
就可以。
推荐阅读
- python - python 3名称错误
- haskell - 在什么情况下我们应该在类型声明中使用 `[ ] a`?
- attributeerror - AttributeError:模块 'Trial1' 没有属性 'Demo'
- javascript - 使用 CSS 或 JavaScript 缩放背景图像
- java - nosuchwindowexception Selenium 与 PhantomJS Java
- django - 错误greenlet docker build
- python - 由于超时,pipenv 安装失败
- reactjs - 动态渲染与混合渲染
- python - 使用 Python 读取、处理和写入文件的最有效(或专业)方法?
- python - x 和 y 的大小必须相同 错误 - K- 均值散点图