Home > OS >  asp.net core web app how to route page requests for index.html and default.html to razor page index.
asp.net core web app how to route page requests for index.html and default.html to razor page index.

Time:01-07

I'm converting a static web site to an asp.net core web app. I've noticed that the index.cshtml razor page is served as /index and /index.html gives a 404 not found, unless added as a static page.

Problem is external sites have indexed direct links to /index.html, /default.html and other pages.

Curious if asp.net core has a better way than adding the static pages with redirect. Like a reverse UseDefaultFiles()

Thanks.

Reasearch has turned up nothing. Lots of results to route default requests to static pages. But I want the reverse. Static page requests to razor pages.

CodePudding user response:

If I don't misunderstand your question, You want to access razor page in the route of /pagename.cshtml and don't want to add static page. You can specify the route name as you want via Route Templates.

For Example, The default code in Privacy is:

@page 
@model PrivacyModel
@{
    ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>

<p>Use this page to detail your site's privacy policy.</p>

If you want to access this page as /privacy.html instead of /privacy, You can specify route after @page.

Change code like:

@page "/privacy.html"
@model PrivacyModel
//.......

Now i can access privacy page in the route of /privacy.html.

Demo:

enter image description here

  • Related