ASP.NET MVC Routing Two GUIDs

I have an action with two GUIDs:

public class MyActionController : Controller
{
  //...

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

I would like the following URI to display the action:

/myaction/movetotab/1/2

... with 1 corresponding to parameter 1 and 2, parameter 2.

What will the route look like, and is it possible to match arguments with parameters with type Guid?

+5
source share
2 answers

Yes, you can match your options with 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 }
);
+7
source

You can take two of your GUIDs as strings and convert them into real GUID objects using the GUID constructor, which takes a string value. Use the route provided by 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);
  }
+3
source

All Articles