Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core RazorPages to force AnchorTagHelper (asp-page) to use lowercase routes

I am using RazorPages in ASP.NET Core v2.0, and I was wondering if anyone knows how to force the AnchorTagHelper to use lowercase?

For example, in my .cshtml mark-up, I would have the following anchor tag using the asp-page tag helper:

<a asp-page="/contact-us">Contact Us</a>

The output that I am looking for

// i want this 
<a href="/contact-us">Contact Us</a>

// i get this 
<a href="/Contact-us">Contact Us</a>

To give it more context to why this is important to my web app, please imagine a long url like what you would see at stackoverflow.com

https://stackoverflow.com/questions/anchortaghelper-asp-page-to-use-lowercase

                       -- VS --

https://stackoverflow.com/Questions/Anchortaghelper-asp-page-to-use-lowercase

Any help would be appreciated!


I have looked at these other references but no luck:

  • https://github.com/aspnet/Mvc/issues/6393
  • How do you enforce lowercase routing in ASP.NET Core MVC 6?
like image 502
Svek Avatar asked Oct 06 '17 04:10

Svek


1 Answers

Add the following to your ConfigureServices method in Startup.cs. It can be added before or after services.AddMvc();

services.AddRouting(options =>
{
    options.LowercaseUrls = true;
});

Now for the anchor tag

<a asp-page="/Contact-Us">Contact Page</a>

And the output

<a href="/contact-us">Contact Page</a>
like image 150
Travis Boatman Avatar answered Sep 22 '22 06:09

Travis Boatman