URL 中的参数过多 - 路由 ASP.NET MVC
我正在寻找管理路由中长网址的最佳方法。我有很多类似这样的操作:
/a/b/c/d/e
路线:
routes.MapRoute(
"xxx",
"{a}/{b}/{c}/{d}/{e}",
new { controller = "Xxx", action="Xxx"});
控制器:
public ActionResult Xxx(int a, int b, int c, int d, int e) { ... }
参数的任何更改都会在每个路线/操作中产生多重更改,这就是问题。它没有弹性。是否有可能将参数映射到某个对象?看起来像这样的东西:
public ActionResult Xxx(RouteParams rp) { ... }
嗯...最终我认为我可以使用操作过滤器来解决这个问题:
private RouteParams rp;
public override void OnActionExecuting(FilterExecutingContext filterContext) {
rp = new RouteParams(...);
}
但我不喜欢这个解决方案
最好的问候
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(FilterExecutingContext filterContext) {
rp = new RouteParams(...);
}
but I dont like this solution
Best regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像您一样创建一个对象并使用 ModelBinder 来构造它而不是过滤器。默认模型绑定器应该可以工作,如果不能,则创建一个自定义模型绑定器。
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.
保持路由设置相同,只需创建一个新模型,其属性与路由设置中的参数匹配:
然后使用 XxxModel 作为操作中的参数:
a、b、c、d 和 e 将映射到模型中的属性。
Keep your route settings the same, just create a new model with properties matching the parameters in the route settings:
Then use XxxModel as your parameter in the action:
a, b, c, d and e will be mapped to the properties in the model.
这对你有用吗?
并且
params 将是一个字符串(例如 1/2/3/4/5)
您必须对参数执行
params.Split("/")
和Convert.ToInt32()
。Will this work for you?
and
params will be one string (eg. 1/2/3/4/5)
you'll have to do a
params.Split("/")
andConvert.ToInt32()
to the parameters.