Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Routing: passing a portion of the route as a "path" parameter

Let's say I have an action like

public ActionResult TheAction(string path) { ... }

What I want to do is to have a request like www.myapp.com/controller/TheAction/path/to/content pass the "path/to/content" part of the route as the "path" parameter to the action.

My guess is that I'd have to fiddle with a custom route/request handler but before donning the complicator's gloves I wanted to see if you guys had any other suggestions.

like image 484
JoseMarmolejos Avatar asked Nov 28 '25 03:11

JoseMarmolejos


1 Answers

Just register /{controller}/{action}/{*path} in your route registration.

That makes the last parameter a catch-all, so it will include the rest of the path, just like you want.

So it will look something like:

        routes.MapRoute(
            "HasCatchAllPath", 
            "{controller}/{action}/{*path}", 
            new { controller = "Home", action = "Index" }  
        );
like image 199
smartcaveman Avatar answered Nov 29 '25 18:11

smartcaveman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!