简体   繁体   中英

Custom route constraint issue in ASP.NET MVC

I am going through "Routing Requests for Disk Files" part in Pro ASP.NET MVC 3 book and am facing a bit of a strange problem relating to a custom route constraint.

I created a custom route:

//82:1. this is added so that below routes are checked first before checking the disk files.
routes.RouteExistingFiles = true;

//82:2 the custom route which maps requests to Static/StaticContent.htm to route /Home/Content, only from IE.
routes.MapRoute("DiskFile", "Content/StaticContent.htm",
    new { controller = "Home", action = "Content" },
    new {customConstraint = new UserAgentConstraint("IE")},
    new[] { "UrlsAndRoutes.Controllers" });

The book says that this route will let IE users view the Home/Content route and non-IE users view the Content/StaticContent.htm directly. It is NOT working for me that way.

Here is what is happening:

  1. I run the application and open the URL http://localhost:50483/Content/StaticContent.htm in IE and am being re-routed to /Home/Content
  2. I then open the URL http://localhost:50483/Content/StaticContent.htm in Chrome and am being shown the original content of the StaticContent.htm
  3. I then change to IE and press Ctrl-R (refresh) OR select URL in url bar and press Enter and am routed to original content of the StaticContent.htm ( why? )
  4. If I press Ctrl-F5 , then, I am being re-routed to /Home/Content ( wut? )

Isn't the route supposed to send IE users, who try to access Content/StaticContent.htm, always to /Home/Content?

PS: I restarted Visual Studio and also deleted browser history on IE, but the issue persists .

UserAgentConstraint.cs
RegisterRoutes method in Global.asax.cs

It's probably being cached

Ctrl+F5 makes it ask server for reload even if content haven't changed.

It sounds like this is correct behaviour.

Verify by making changes to static.html after first reload. Then Ctrl+R reload. It should hit the action method.

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