【问题标题】:How to add extension .html in url asp.net mvc 4?如何在 url asp.net mvc 4 中添加扩展名 .html?
【发布时间】:2015-05-30 02:59:20
【问题描述】:

我有网址: http://localhost:1714/Message/Index

我想展示: http://localhost:1714/Message/Index.html

我该怎么做?

【问题讨论】:

  • 你可以通过在.htaccess文件iis.net/learn/extensions/url-rewrite-module/…中编写url重写方法来做到这一点
  • 为什么要这样做? MVC 路由映射的整个想法是将实现隐藏在整洁的 URL 后面。
  • 我只是想自定义网址。我厌倦了查看没有扩展名的 URL。

标签: asp.net asp.net-mvc routes web-config asp.net-mvc-routing


【解决方案1】:

您需要修改 Web.config 以将对您的 HTML 文件的请求映射到 TransferRequestHandler。

像这样:

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

这由 Jon Galloway here 解释。

然后把它放到你的 RouteConfig 中:

public static void RegisterRoutes(RouteCollection routes)
        {
            ...
            routes.MapRoute("Default", "{controller}/{action}.html", new { controller = "Home", action = "Index" });
            ...
        }

访问http://localhost:{port}/Home/Index.html 会将您转到您的主页。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2014-02-09
    • 2010-10-29
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多