MVC路由匹配除资源之外的所有路由

时间:2014-08-23 08:00:48

标签: c# asp.net-mvc asp.net-mvc-4

我是一个angularJS应用程序。我已经创建了一个控制器/动作,可以在任何请求中返回呈现的index.cshtml。这是我唯一的mvc4控制器。我使用webapi进行所有其他行为。客户端应该只处理路由。到目前为止运作良好。

RouteExistingFiles = false;

routes.MapRoute(
    name: "Default",
    url: "{*url}",
    defaults: new { controller = "Index", action = "Index" }
);

问题是此解决方案匹配像" localhost / favicon.ico,localhost / template.html"这样的资源。 我可以阻止或忽略这样的所有请求吗?

2 个答案:

答案 0 :(得分:3)

您可以使用IgnoreRoute,以便路由模块忽略对这些资源的请求,例如:

routes.IgnoreRoute("favicon.ico");
routes.IgnoreRoute("{templateName}.html");

这些必须在您当前的Default路线之前添加。

答案 1 :(得分:2)

您可以尝试这样的操作,然后路由模块会忽略对任何 .html .ico 文件的请求: -

routes.IgnoreRoute("{*allhtml}", new {allhtml=@".*\.html(/.*)?"});
routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"})