在使用ASP.NET MVC(3或4DP)的外的现成方法定位器,是有办法有一个字符串和GUID之间的MVC架构区分,而无需解析控制器操作中的参数?微分GUID和string参数在MVC 3
用法的例子是用于URL
http://[domain]/customer/details/F325A917-04F4-4562-B104-AF193C41FA78
执行
public ActionResult Details(Guid guid)
方法,和
执行
public ActionResult Details(string id)
方法。
由于没有改变,显然这些方法是不明确的,具体如下:
public ActionResult Details(Guid id)
{
var model = Context.GetData(id);
return View(model);
}
public ActionResult Details(string id)
{
var model = Context.GetData(id);
return View(model);
}
导致错误:
The current request for action 'Details' on controller type 'DataController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Details(System.Guid) on type Example.Web.Controllers.DataController
System.Web.Mvc.ActionResult Details(System.String) on type Example.Web.Controllers.DataController
我试图使用自定义的约束(基于How can I create a route constraint of type System.Guid?)尝试并通过路由推送它:
routes.MapRoute(
"Guid",
"{controller}/{action}/{guid}",
new { controller = "Home", action = "Index" },
new { guid = new GuidConstraint() }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
并切换动作签名:
public ActionResult Details(Guid guid)
{
var model = Context.GetData(guid);
return View(model);
}
public ActionResult Details(string id)
{
var model = Context.GetData(id);
return View(model);
}
约束执行和通过,从而将参数发送到一个动作,但貌似还作为一个字符串,因此不明确的两个方法签名。我预计操作方法的定位会导致模糊,因此可以通过插入定制模块来定位方法来覆盖。
同样的结果可以通过解析出字符串参数来实现,但将是非常好的为简便起见,以避免在行动(更不用说希望有一天以后再使用),其逻辑。
你是对的,动作方法定位器是浑然不觉的路由约束。如果删除字符串操作方法,那么guid操作方法是否被选中? – bzlm
MVC不支持仅基于签名的方法重载 - 最简单的解决方案可能是简单地使用两个唯一命名的操作方法,一个用于GUID(Details)的详细信息,另一个用于获取详细信息(搜索或信息) ?)。 – Tommy
@bzlm - 更正,删除字符串动作将选择Guid(假设它传递约束或可以被解析成Guid)。 – falquan