我有一个采取两个GUID的行动:
public class MyActionController : Controller
{
//...
public ActionResult MoveToTab(Guid param1, Guid param2)
{
//...
}
}
我希望将以下URI映射到操作:
/myaction/movetotab/1/2
... 1对应于param1,2对应param2。
路线是什么样的,是否可以将参数映射到具有Guid类型的参数?
答案 0 :(得分:7)
是的,您可以将参数映射到System.Guid
routes.MapRoute(
"MoveToTab",
"{controller}/{action}/{param1}/{param2}",
new { controller = "MyAction", action = "MoveToTab",
param1 = System.Guid.Empty, param2 = System.Guid.Empty }
);
或
routes.MapRoute(
"MoveToTab2",
"myaction/movetotab/{param1}/{param2}",
new { controller = "MyAction", action = "MoveToTab",
param1 = System.Guid.Empty, param2 = System.Guid.Empty }
);
答案 1 :(得分:3)
您可以将两个GUID作为字符串接收,并使用接受字符串值的GUID构造函数将它们转换为实际的GUID对象。使用eu-ge-ne提供的路线。
routes.MapRoute(
"MoveToTab",
"myaction/movetotab/{param1}/{param2}",
new { controller = "MyAction", action = "MoveToTab", param1 = "", param2 = "" }
);
public ActionResult MoveToTab(string param1, string param2)
{
Guid guid1 = new Guid(param1);
Guid guid2 = new Guid(param2);
}