【问题标题】:Why are two ActionResult methods being invoked for my page?为什么要为我的页面调用两个 ActionResult 方法?
【发布时间】:2010-12-08 20:16:42
【问题描述】:

我有一个 asp.net-mvc 应用程序,其中包含由我的“用户”控制器提供的以下 aspx 页面:Index.aspx、User.aspx、UsersIntoActivity.aspx 和 UsersUsingLocation.aspx。

在我的 Global.ashx.cs 中,我设置了以下路线:

routes.MapRoute(
            "UsersHome",
            "Users",
            new { controller = "Users", action = "Index" });

routes.MapRoute(
            "Users",
            "Users/{id}/{name}",
            new { controller = "Users", action = "User", id = "", name = "" });

routes.MapRoute(
            "UsersUsing",
            "Users/Using/{locationId}/{name}",
            new { controller = "Users", action = "UsersUsingLocation", locationId = "", name = "" });

routes.MapRoute(
            "UsersInto",
            "Users/Into/{activityId}/{name}",
            new { controller = "Users", action = "UsersIntoActivity", activityId = "", name = "" });

问题是,当我尝试通过 url 'website/Users/Into/1/some-activity' 或 website/Users/Using/1/some-location 访问 UsersIntoActivity.aspx 或 UsersUsingLocation.aspx 时,适当的 ActionResult方法正在被调用,但 User 方法也会在之后被调用。

【问题讨论】:

    标签: asp.net-mvc friendly-url


    【解决方案1】:

    我想由于某种原因,对该路由的第二个请求来自浏览器。尝试使用Fiddler2 捕获浏览器和 Web 服务器之间的流量。这可以帮助您排除正在发生 301/302 重定向或呈现的 HTML 中存在对其他资源的引用的可能性(例如在图像、脚本或链接标记中)。

    【讨论】:

    • 很有趣,您应该提到图像或其他资源,因为当调用 User(id, name) 方法时,参数类似于“Images”和“MySiteLogo.png”,这很奇怪。
    • David Liddle 的回答应该能解决这个问题。
    • 另见这篇文章:blog.codeville.net/2008/07/07/…
    • 还有一件事,有没有办法让 Fiddler 在从 Visual Studio 调试我的网站时捕获流量?
    【解决方案2】:

    根据 gWiz 的回复,作为一般规则,在 IIS(适用于 IIS 5 和 6)中应用映射扩展后,我然后右键单击图像和/或脚本文件夹的属性,然后删除您刚刚添加的 aspnet_isapi 扩展.现在将不会为这些文件夹中的文件请求调用 MVC 路由。

    或者在你的全局 asax 中设置一个忽略路由扩展。

    this.Routes.IgnoreRoute("Content/{*pathInfo}");
    //e.g if your images, css, scripts are in a content folder
    

    【讨论】:

      【解决方案3】:

      考虑使用路由调试器检查您的路由:

      http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

      这应该会给你答案,真正的名字是什么。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-10
        • 1970-01-01
        相关资源
        最近更新 更多