MVC路由匹配除资源之外的所有路由(MVC Routing match all routes except resources)

编程入门 行业动态 更新时间:2024-10-15 16:18:12
MVC路由匹配除资源之外的所有路由(MVC Routing match all routes except resources)

我有一个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”等资源。 我可以阻止或忽略这样的所有请求吗?

I've a angularJS application. I've created a controller/action that returns a rendered index.cshtml on any request. This is my only mvc4 controller. I use the webapi for all other behavior. The client-side should only handling the routes. Works well so far.

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

The problem is that this solution matches resources like "localhost/favicon.ico, localhost/template.html". Can i prevent or ignore all request like this ?

最满意答案

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

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

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

You could use IgnoreRoute so the routing module ignores requests for those resources, for example:

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

These have to be added before your current Default route.

更多推荐

本文发布于:2023-08-01 22:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1365786.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   资源   MVC   Routing   resources

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!