开发者

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

开发者 https://www.devze.com 2023-02-16 11:34 出处:网络
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

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.


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

精彩评论

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