将.html与 MVC 4 应用一起使用



我有一个我做的控制器叫做test,去website.com/test工作得很好。如果我尝试转到website.com/test.html,如何使同一控制器响应?

例如

AttributeRouting 可以做到这一点

例:

 [GET("test")]
 [GET("test.html")]
 public ActionResult test()
 {
     return view();
 }

http://attributerouting.net/

RouteConfig.cs

查看的地方。

解决方案 1:IgnoreRoute

在您的RouteConfig.cs中,在RegisterRoutes(RouteCollection routes)下:

  routes.IgnoreRoute("*.html");

请确保您的IgnoreRoute声明排在所有其他MapRoute声明之前。

解决方案 2:MapRoute

routes.MapRoute(
    name: "HtmlUrl",
    url: "{action}.html",
    defaults: new { controller = "Home"}
);

最新更新