views:

62

answers:

1

Hello,

lets say I have a controller named Store. There is an index method:

public ActionResult Index(string val, long? id, int? id2)
{
  return View();
}

I want to fetch things like:

/my-store/some-text 
/my-store/some-text/
/my-store/some-text/123456789012335
/my-store/some-other-text/4562343462345
/my-store/some-other-bla-text/4562343462345/1
/my-store/some-other-bla-bla-text/45345723554/2

So the requerements are:
+ the controller link-text is "my-store" and has to route to Store controller
+ if there is only some text and id, then just leave id2 null
+ if there is only some text and no id, then just leave id and id2 null

Is the following correct?

routes.MapRoute(
    "Store", // Route name
    "my-store/{val}/{id}/{id2}", // URL with parameters
    new { controller = "Store", action = "Index", id = UrlParameter.Optional, id2 = UrlParameter.Optional }
);

Thank you for your help in advance!

+2  A: 

Yeah looks good except you need to define a default value for {val} in your route:

routes.MapRoute(
"Store", // Route name
"my-store/{val}/{id}/{id2}", // URL with parameters
new { controller = "Store", action = "Index", val = "", id = UrlParameter.Optional, id2 = UrlParameter.Optional }

Just remember to cleanse the string in val as if you have some funky characters then your link won't work. Like let's say val equal "thisis/val", then your link will be:

/my-store/thisis/val/1212/12

Which would not hit your route and most likely return a 404

amurra