Too many params in URL - Routing ASP.NET MVC
Im searching the best way for manage long urls in routing. I have many actions which looks like this:
/a/b/c/d/e
the route:
routes.MapRoute(
"xxx",
"{a}/{b}/{c}/{d}/{e}",
new { controller = "Xxx", action="Xxx"});
the controller:
public ActionResult Xxx(int a, int b, int c, int d, int e) { ... }
any change in params gives multi-change in every route/action, and that is the problem. Its not elastic. Is there any possibility to map params to some object? Something that would look like:
public ActionResult Xxx(RouteParams rp) { ... }
Hmm... eventually I think that I could use the Action Filter to solve this:
private RouteParams rp;
public override void OnActionExecuting(FilterEx开发者_运维问答ecutingContext filterContext) {
rp = new RouteParams(...);
}
but I dont like this solution
Best regards
Create an object like you did and use ModelBinder to construct it instead of filter. The Default Model binder should work, if not then create a custom one.
Keep your route settings the same, just create a new model with properties matching the parameters in the route settings:
public class XxxModel
{
public int a { get; set; }
public int b { get; set; }
public int c { get; set; }
public int d { get; set; }
public int e { get; set; }
}
Then use XxxModel as your parameter in the action:
public ActionResult Xxx( XxxModel model )
{
...
}
a, b, c, d and e will be mapped to the properties in the model.
Will this work for you?
routes.MapRoute(
"xxx",
"{*params}",
new { controller = "Xxx", action="Xxx"});
and
public ActionResult Xxx(string params) { ... }
params will be one string (eg. 1/2/3/4/5)
you'll have to do a params.Split("/")
and Convert.ToInt32()
to the parameters.
精彩评论