1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I did what you suggested and I could see the correct output in IE after the Ctrl-R reload, meaning that it hit the action method. Immediately after this reload, I hit Ctrl-R in Chrome and then in IE, and the problem came back. IE also showed the modified StaticContent.htm. I have even opened IE in Private Mode, I should add.
Use a tool to investigate the HTTP response code (like developer tools in chrome, tab "network"). I'm sure you'll see a "304 Not Modified" response for those IE requests where the action method are not hit. Correct?
I used the similar tool in IE (pressing F12 brings it up) to see that when hit Ctrl-R, I get a status of '304 Not Modified' and for Ctrl-F5 a status of 200 on all resources loaded. What can be inferred from the fact that I am getting 304 on Ctrl-R refresh?
Exactly. See the flow here. The url is "/staticpage.htm". First load is a 200 OK and the request are processed by the server and the routing rules are applied and gets the (IE) request to route to the action method. Next request (ctrl+r) becomes a 301 as "/staticpage.htm" hasnt changed since last request. Url routes are not being applied as server already decided to send a 301. Reloading with "ctrl + F5" on the other hand sends a "send me fresh content no matter what"-request results in a 200 OK response where the routing where used. See the flow? Correct?
I moved on from this and was working on something else, the caching somehow stopped after sometime and I was able to steadily see the correct output in IE. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.