当我启动 ASP.NET MVC2 应用程序时,为什么会调用错误的控制器?
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然,因为它匹配第一个根。
事情是 - 当您提供默认值时 - 它们就变成可选的。如果每个路线数据值都是可选的并且路线是第一个 - 则保证它将第一个命中。
像这样的东西应该有效:
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:
从 Metrics 路由中删除默认值:
使用默认值,MVC 几乎可以将您传递给它的任何 URL 映射到 Metric 控制器中的 GetMetrics 操作。
Remove the defaults from your Metrics route:
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.
简而言之:它使用
Matrics
路由,因为它与Matrics
路由匹配。简而言之:默认路由定义了所有路由组件的默认值,并且所有路由组件都是可选的。您的
Metrics
路由所做的就是添加另一个带有默认值的可选路由参数...它基本上与默认路由没有什么不同,因为整个路由包含可选参数。如果您希望它正常工作,您需要将您的
Metrics
路由与默认路由区分开来。例如
HTH,
查尔斯
旁注:
让我们从不同的角度来看这个问题 - url 中的哪些内容与默认路由中指定的 url 模式相匹配?
In short: it's using the
Matrics
route because it matches theMatrics
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.
HTHs,
Charles
Side note:
Let's look at this from a different angle - what in the url matches the url pattern specified in the default route?