2013-09-27 26 views
1

我有一個控制器,我叫做test,去website.com/test工作得很好。如果我嘗試去website.com/test.html,如何讓同一控制器響應?使用.html和MVC 4應用

+0

http://weblogs.asp.net/jgalloway/archive/2013/08/29/asp-net-mvc-routing-intercepting-file-requests-like- index-html-and-what-it-teachhes-about-how-routing-works.aspx –

回答

1

AttributeRouting例如可以做到這一點

實施例:

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

http://attributerouting.net/

+0

這對我有效。 – Lysandus

1

RouteConfig.cs是看的地方。

解決方案1:IgnoreRoute

在你RouteConfig.csRegisterRoutes(RouteCollection routes)下:

routes.IgnoreRoute("*.html"); 

請確保您的IgnoreRoute說法是領先於所有其他MapRoute聲明的地方。

解決方案2:MapRoute

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