开发者

ASP.NET MVC Routing two GUIDs

开发者 https://www.devze.com 2022-12-16 14:21 出处:网络
I have an action taking two GUIDs: public class MyActionController : Controller { //... public ActionResult MoveToTab(Guid param1, Guid param2)

I have an action taking two GUIDs:

public class MyActionController : Controller
{
  //...

  public ActionResult MoveToTab(Guid param1, Guid param2)
  {
    //...
  }
}

I would like the following URI to map to the action:

/myaction/movetotab/1/2

...with 1 corresponding to param1 and 2 to param2.

What will th开发者_如何学Ce route look like, and is it possible to map the arguments to parameters with a type of Guid?


Yes, it is possible to map your parameters to System.Guid

routes.MapRoute(
    "MoveToTab",
    "{controller}/{action}/{param1}/{param2}",
    new { controller = "MyAction", action = "MoveToTab",
        param1 = System.Guid.Empty, param2 = System.Guid.Empty }
);

or

routes.MapRoute(
    "MoveToTab2",
    "myaction/movetotab/{param1}/{param2}",
    new { controller = "MyAction", action = "MoveToTab",
        param1 = System.Guid.Empty, param2 = System.Guid.Empty }
);


You can take in your two GUIDs as strings, and convert them to actual GUID objects using the GUID constructor that accepts a string value. Use the route that eu-ge-ne provided.

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);
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消