当我启动 ASP.NET MVC2 应用程序时,为什么会调用错误的控制器?

发布于 2024-09-26 13:27:06 字数 990 浏览 3 评论 0 原文

我有一个名为 MetricsController 的控制器,具有单个操作方法:

public class MetricsController
{
  public ActionResult GetMetrics(int id, string period)
  {
    return View("Metrics");
  }
}

我想将调用路由到此控制器,如下所示:

http://mysite/metrics/getmetrics/123/24h

我在 Global.asax.cs 中映射了一条附加路由,如下所示:

routes.MapRoute(
  "Metrics",
  "{controller}/{action}/{id}/{period}",
  new { controller = "Metrics", action = "GetMetrics", id = 0, period = "" }
);

routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

我刚刚将其添加到 Visual Studio 2010 创建的默认模板项目中。

当我运行应用程序时,它不是默认使用 HomeController,而是在 MetricsController 中启动。

为什么会发生这种情况?当我启动应用程序时,url 中没有任何内容与 Metrics 路由中指定的 url 模式相匹配。

这一切都在 Visual Studio 2010 中使用内置 Web 服务器进行了尝试。

I have a controller called MetricsController with a single action method:

public class MetricsController
{
  public ActionResult GetMetrics(int id, string period)
  {
    return View("Metrics");
  }
}

I want to route calls to this controller like this:

http://mysite/metrics/getmetrics/123/24h

I've mapped an additional route in my Global.asax.cs like this:

routes.MapRoute(
  "Metrics",
  "{controller}/{action}/{id}/{period}",
  new { controller = "Metrics", action = "GetMetrics", id = 0, period = "" }
);

routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

I just added this to the default template project that Visual Studio 2010 creates.

When I run the application, instead of defaulting to the HomeController, it starts in the MetricsController instead.

Why is this happening? There's nothing in the url when I start the application that matches the url pattern specified in the Metrics route.

This is all being tried in Visual Studio 2010 using the built-in web server.

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

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

发布评论

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

评论(3

泪之魂 2024-10-03 13:27:06

当然,因为它匹配第一个根。

事情是 - 当您提供默认值时 - 它们就变成可选的。如果每个路线数据值都是可选的并且路线是第一个 - 则保证它将第一个命中。

像这样的东西应该有效:

routes.MapRoute(
  "Metrics",
  "Metrics/GetMetrics/{id}/{period}",
  //assuming id, period aren't supposed to be optional
  new { controller = "Metrics", action = "GetMetrics" }      
);

routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Because it matches first root, of course.

Thing is - when You provide default values - they become optional. If every one of routedata values are optional and route is first - it's guaranteed that it will hit first.

Something like this should work:

routes.MapRoute(
  "Metrics",
  "Metrics/GetMetrics/{id}/{period}",
  //assuming id, period aren't supposed to be optional
  new { controller = "Metrics", action = "GetMetrics" }      
);

routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
三生一梦 2024-10-03 13:27:06

从 Metrics 路由中删除默认值:


routes.MapRoute(
  "Metrics",
  "{controller}/{action}/{id}/{period}",
  new { controller = @"Metrics", action = "GetMetrics"}
);

使用默认值,MVC 几乎可以将您传递给它的任何 URL 映射到 Metric 控制器中的 GetMetrics 操作。

Remove the defaults from your Metrics route:


routes.MapRoute(
  "Metrics",
  "{controller}/{action}/{id}/{period}",
  new { controller = @"Metrics", action = "GetMetrics"}
);

With the default values MVC is able to map to the GetMetrics action in the Metric controller pretty much any URL that you pass to it.

薔薇婲 2024-10-03 13:27:06

简而言之:它使用 Matrics 路由,因为它与 Matrics 路由匹配。

简而言之:默认路由定义了所有路由组件的默认值,并且所有路由组件都是可选的。您的 Metrics 路由所做的就是添加另一个带有默认值的可选路由参数...它基本上与默认路由没有什么不同,因为整个路由包含可选参数。

如果您希望它正常工作,您需要将您的 Metrics 路由与默认路由区分开来。

例如

routes.MapRoute(
  "Metrics",
  "metrics/{action}/{id}/{period}",
  new { controller = @"Metrics", action = "GetMetrics", id = 0, period = "" }
);

HTH,
查尔斯

旁注:

什么都没有
在我启动时的网址中
与 url 匹配的应用程序
指标中指定的模式
路线。

让我们从不同的角度来看这个问题 - url 中的哪些内容与默认路由中指定的 url 模式相匹配?

In short: it's using the Matrics route because it matches the Matrics route.

In long: The default route defines defaults for all the route components and all of the route components are optional. All that your Metrics route is doing is adding another optional route parameter with a default... it's basically no different from the default route because the whole route contains optional parameters.

If you want it to work, you need to differentiate your Metrics route from the default route.

E.g.

routes.MapRoute(
  "Metrics",
  "metrics/{action}/{id}/{period}",
  new { controller = @"Metrics", action = "GetMetrics", id = 0, period = "" }
);

HTHs,
Charles

Side note:

There's nothing
in the url when I start the
application that matches the url
pattern specified in the Metrics
route.

Let's look at this from a different angle - what in the url matches the url pattern specified in the default route?

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