开发者

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.


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜