简体   繁体   中英

How does MVC routing detects file path

As we know that routing follows the routes provided in RegisterRoutes function in RouteConfig.cs which defines the controller and action in default routes.

My project only have default route like:

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

So how does routing detects that whether we are accessing some action or pointing to a file like image, css or js.

The short answer is that any request for a resource that has a . in its name is directly served by IIS without invoking MVC routing. Conversely, requests that do not contain a . are passed to MVC for routing. Importantly this is just default behaviour.

As a quick test, fabricate a url for any resource that does not exist in your project such as \\asdf and note the 404 error generated by MVC. Now repeat the test with \\asdf.asdf and note the 404 error is generated by IIS.

It is possible to change this behaviour with directives in web.config. For example, see How do I route images using ASP.Net MVC routing?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM