ASP.NET MVC中的自定义路由约束问题

时间:2013-04-02 14:03:35

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

我正在阅读Pro ASP.NET MVC 3书中的“磁盘文件的路由请求”部分,并且面临一个与自定义路由约束相关的奇怪问题。

我创建了一个自定义路线:

//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" });

书中说这条路线将让IE用户查看Home / Content路由,非IE用户直接查看Content / StaticContent.htm。 这对我不起作用。

以下是发生的事情:

  1. 我运行应用程序并在IE中打开网址http://localhost:50483/Content/StaticContent.htm,然后重新路由到/ Home / Content
  2. 然后我在Chrome中打开了网址http://localhost:50483/Content/StaticContent.htm,并显示了StaticContent.htm的原始内容
  3. 然后我更改为IE并按Ctrl-R(刷新)或在网址栏中选择网址并按Enter,然后路由到StaticContent.htm的原始内容(为什么?
  4. 如果我按Ctrl-F5,那么,我将被重新路由到/ Home / Content( wut?
  5. 路由是否应该发送IE用户,他们尝试访问Content / StaticContent.htm,总是发送到/ Home / Content?

    PS:我重新启动了Visual Studio并删除了IE上的浏览器历史记录,但问题仍然存在

    UserAgentConstraint.cs
    RegisterRoutes method in Global.asax.cs

1 个答案:

答案 0 :(得分:1)

可能正在缓存

Ctrl + F5会让服务器重新加载,即使内容没有更改。

听起来这是正确的行为。

首次重新加载后,通过更改static.html进行验证。然后按Ctrl + R重新加载。它应该点击动作方法。

相关问题