Sve*_*vek 7 c# asp.net-core razor-pages asp.net-core-mvc-2.0
我在ASP.NET Core v2.0中使用RazorPages,我想知道是否有人知道如何强制AnchorTagHelper使用小写?
例如,在我的.cshtml标记中,我将使用asp-page标记帮助程序获得以下锚标记:
<a asp-page="/contact-us">Contact Us</a>
Run Code Online (Sandbox Code Playgroud)
我正在寻找的输出
// i want this
<a href="/contact-us">Contact Us</a>
// i get this
<a href="/Contact-us">Contact Us</a>
Run Code Online (Sandbox Code Playgroud)
为了更好地说明为什么这对我的网络应用程序很重要,请想象一下你会在stackoverflow.com上看到的长网址.
https://stackoverflow.com/questions/anchortaghelper-asp-page-to-use-lowercase
-- VS --
https://stackoverflow.com/Questions/Anchortaghelper-asp-page-to-use-lowercase
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!
我看过这些其他参考文献,但没有运气:
Tra*_*man 11
将以下内容添加到您的ConfigureServices方法中Startup.cs.它可以在之前或之后添加services.AddMvc();
services.AddRouting(options =>
{
options.LowercaseUrls = true;
});
Run Code Online (Sandbox Code Playgroud)
现在为锚标签
<a asp-page="/Contact-Us">Contact Page</a>
Run Code Online (Sandbox Code Playgroud)
和输出
<a href="/contact-us">Contact Page</a>
Run Code Online (Sandbox Code Playgroud)
小智 5
现在更容易了,只需将其添加到 Startup.cs 中的 ConfigureServices 方法即可。
services.Configure<RouteOptions>(options =>
{
options.LowercaseUrls = true;
});
Run Code Online (Sandbox Code Playgroud)
将生成目标链接的 url 帮助程序将使用为路由生成的元数据。对于 razor 页面,这使用确切的文件路径来识别路由。
\n\n从 ASP.NET Core 2.1 开始,您可能能够像使用Route属性的 MVC 路由一样调整路由。至少它\xe2\x80\x99s计划用于2.1版本。
在此之前,您必须在ConfigureServices方法中为页面配置特定路由:
services.AddMvc()\n .AddRazorPagesOptions(options =>\n {\n options.Conventions.AddPageRoute("/ContactUs", "contact-us");\n });\nRun Code Online (Sandbox Code Playgroud)\n\n这将使现有路由可供使用,因此如果您不希望这样,则需要替换此页面的路由选择器。没有内置的方法可以做到这一点,但是当您知道要做什么时,\xe2\x80\x99 并不难做到。
\n\n为了避免代码重复并使一切变得更好,我们\xe2\x80\x99ll只创建一个ReplacePageRoute扩展方法,它基本上与上面的用法相匹配AddPageRoute:
services.AddMvc()\n .AddRazorPagesOptions(options => {\n options.Conventions.ReplacePageRoute("/Contact", "contact-us");\n });\nRun Code Online (Sandbox Code Playgroud)\n\n该方法的实现直接受到以下启发AddPageRoute:
public static PageConventionCollection ReplacePageRoute(this PageConventionCollection conventions, string pageName, string route)\n{\n if (conventions == null)\n throw new ArgumentNullException(nameof(conventions));\n if (string.IsNullOrEmpty(pageName))\n throw new ArgumentNullException(nameof(pageName));\n if (route == null)\n throw new ArgumentNullException(nameof(route));\n\n conventions.AddPageRouteModelConvention(pageName, model =>\n {\n // clear all existing selectors\n model.Selectors.Clear();\n\n // add a single selector for the new route\n model.Selectors.Add(new SelectorModel\n {\n AttributeRouteModel = new AttributeRouteModel\n {\n Template = route,\n }\n });\n });\n\n return conventions;\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
685 次 |
| 最近记录: |