在我的MVC 4应用程序中,我使用了Area,这是我的路线:
context.MapRoute(
"Service_AcceptBid",
"Service/{controller}/{action}/{id}/{accept}",
new { area = "Service", controller = "Project", action = "AcceptBid", id = UrlParameter.Optional, accept = UrlParameter.Optional }
);
context.MapRoute(
"Service_BidNegotiation",
"Service/{controller}/{action}/{id}/{bid}",
new { area = "Service", controller = "Project", action = "Negotiations", id = UrlParameter.Optional, bid = UrlParameter.Optional }
);
我将这些链接放在.cshtml页面上:
<div dir="ltr">
@Html.RouteLink("Accept", "Service_AcceptBid", new { controller = "Project", action = "AcceptBid", Id = item.ProjectId, Accept = item.Id })
@Html.RouteLink("Negotiate", "Service_BidNegotiation", new { controller = "Project", action = "Negotiations", Id = item.ProjectId, Bid = item.Id })
</div>
单击第一个链接时,调用了AcceptBid控制器,并将其参数(Id,Accept)设置为url值.但是,当我单击第二个链接时,控制器方法中的ID参数已设置,但第二个参数(Bid)为空.
为什么会这样呢?
我该怎么做才能解决此问题?
谢谢.
解决方法:
解释问题
输出路由(链接生成)很好.问题在于传入路由.当您单击应该由第一条路线处理的第二个链接时,它实际上将由第一条路线处理,因为它匹配其定义.
可能的解决方案(无硬编码操作)
您要做的就是为第一个路由提供其他约束,以防止它处理应由第二个路由处理的请求.
检查以下代码更改,在其中在action参数上添加路由约束以提供可能的值的地方:
context.MapRoute(
"Service_AcceptBid",
"Service/{controller}/{action}/{id}/{accept}",
new { area = "Service", controller = "Project", action = "AcceptBid", id = UrlParameter.Optional, accept = UrlParameter.Optional },
new { action = "AcceptBid|RejectBid|..." }
);
context.MapRoute(
"Service_BidNegotiation",
"Service/{controller}/{action}/{id}/{bid}",
new { area = "Service", controller = "Project", action = "Negotiations", id = UrlParameter.Optional, bid = UrlParameter.Optional }
);
与为每个单独的动作创建静态路由定义相比,这将使您的路由更具动态性.