【发布时间】:2014-04-23 22:15:24
【问题描述】:
在我的 routeConfig 中,我在默认路由的顶部添加了一个自定义路由
routes.MapRoute(
name: "appointmentandattendee",
url: "{controller}/{action}/{appointmentId}/{attendeeId}",
defaults: new { controller = "Response", action = "Index", appointmentId = UrlParameter.Optional, attendeeId = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
所以,对于这个动作
public ActionResult Index(Guid appointmentId, Guid attendeeId)
{
return Content("Hello");
}
路由看起来像
http://localhost/Response/Index/96de7851-49f6-4b69-8a58-2bea39bd466e/7d4fe8ed-7dae-e311-be8f-001c42aef0b2
但现在名称为 Default 的路由被忽略了
例如:
在我的约会控制器中,有一些动作和参数作为 id 我期待使用默认路由,但它没有
所以,对于AppointmentController中的这个动作
public ActionResult Test(Guid id)
{
return Content("Hello");
}
路由看起来像
localhost:/Appointment/Test?id=96de7851-49f6-4b69-8a58-2bea39bd466e
这是为什么呢? 在这种情况下不应该使用默认路由吗?
【问题讨论】:
-
这是因为您将
appointmentId和attendeeId参数声明为可选。现在这是您路线列表中最通用的路线。您确定要它们是可选的吗?他们的目的不是总是传递给你的行动吗? -
即使我做了
defaults: new { controller = "Response", action = "Index", appointmentId = "", attendeeId = "" }也没有用。我最终做的是url: "Response/{appointmentId}/{attendeeId}"所以控制器应该是响应
标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing asp.net-mvc-5