Friday, August 3, 2018

How to add .html url suffix to all Razor pages in ASP.NET Core 2?

Leave a Comment

I have an ASP.NET Core 2.0 MVC app using Razor pages stored in the project tree under the /Pages folder.

The project is set up using default routing etc.

I would like to all the Razor pages to have .html extension in the browser's address bar.

File /Pages/test.cshtml is currently available via http://localhost/test. I would like this page to be available via http://localhost/test.html instead.

I tried to understand the routing settings but failed to understand it enough to come with the solution.

What should I write where so the files are available via .html suffix?

1 Answers

Answers 1

defined a route for this as follows

routes.MapRoute( "MyHtml", "{controller}/{action}.html", new { controller = "Home", action = "Index" }); 

and add HtmlFileHandler to web.config. And .html routes work now.

<handlers>   <add name="HtmlFileHandler" path="*.html" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> </handlers> 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment