为什么我的页面会调用两个 ActionResult 方法?

发布于 2024-08-08 01:03:27 字数 1041 浏览 11 评论 0原文

我有一个 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' 访问 UsersIntoActivity.aspx 或 UsersUsingLocation.aspx 时website/Users/Using/1/some-location 正在调用相应的 ActionResult 方法,但随后也会调用 User 方法。

I have an asp.net-mvc app with the following aspx pages served by my 'Users' controller: Index.aspx, User.aspx, UsersIntoActivity.aspx and UsersUsingLocation.aspx.

In my Global.ashx.cs I've got the following routes set up:

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

The problem is that when I try to access UsersIntoActivity.aspx or UsersUsingLocation.aspx via the urls 'website/Users/Into/1/some-activity' or website/Users/Using/1/some-location the appropriate ActionResult method is being invoked, but the User method also gets invoked afterwards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的夜不怕黑 2024-08-15 01:03:27

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

I would imagine a second request to that route is coming from the browser for some reason. Try capturing the traffic between the browser and the web server using Fiddler2. This can help you rule-out the possibility that a 301/302 redirect is occurring or references to other resources exist in the rendered HTML (such as in an image, script, or link tag).

偷得浮生 2024-08-15 01:03:27

根据 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

Following on from gWiz's reply, as a general rule after applying the mapping extension in IIS (for IIS 5 and 6), I then right click the properties of image and/or script FOLDERS and then remove the aspnet_isapi extension you just added. MVC routes will now not be called for file requests in these folders.

Alternatively setup an ignore route extension in your global asax.

this.Routes.IgnoreRoute("Content/{*pathInfo}");
//e.g if your images, css, scripts are in a content folder
悲欢浪云 2024-08-15 01:03:27

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

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

这应该给你答案,真正叫什么。

Consider using route debugger to check your routes:

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

That should give you answer, what is really called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文