简体   繁体   English

如何在ASP.NET Core 2中为所有Razor页面添加.html url后缀?

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

I have an ASP.NET Core 2.0 MVC app using Razor pages stored in the project tree under the /Pages folder. 我有一个ASP.NET Core 2.0 MVC应用程序使用存储在/ Pages文件夹下的项目树中的Razor页面。

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. 我希望所有Razor页面在浏览器的地址栏中都有.html扩展名。

File /Pages/test.cshtml is currently available via http://localhost/test . 文件/Pages/test.cshtml目前可通过http:// localhost / test获得 I would like this page to be available via http://localhost/test.html instead. 我希望通过http://localhost/test.html提供此页面。

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? 我应该写什么,以便通过.html后缀提供文件?

defined a route for this as follows 为此定义了一条路线,如下所示

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

and add HtmlFileHandler to web.config. 并将HtmlFileHandler添加到web.config。 And .html routes work now. 而且.html路线现在可以工作了。

<handlers>
  <add name="HtmlFileHandler" path="*.html" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM