如何使用两个可选参数(其中一个是参数数组值)设置 ASP.NET MVC 路由?
我想制作以下 asp.net mvc 路由:
http://somedomain.com/user/search/500?Users=1,2,3,4
http://somedomain.com/user/search/500
http://somedomain.com/user/search?Users=1,2,3,4
http://somedomain.com/user/search
用户将与控制器匹配,搜索将与操作方法匹配。可选参数 500 与您猜测的操作方法中的可选参数相匹配。用户的可选查询字符串将与操作方法中的可选数组参数匹配。
设置这些的最佳方法是什么?自定义 ActionFilterAttribute?两种不同的动作方法?我的路线集合中有多个路线条目?
任何信息将不胜感激。
I want to make the following asp.net mvc routes:
http://somedomain.com/user/search/500?Users=1,2,3,4
http://somedomain.com/user/search/500
http://somedomain.com/user/search?Users=1,2,3,4
http://somedomain.com/user/search
User would match to the controller, search would match to the action method. The optional parameter 500 would match to you guessed it an optional parameter in the action method. The optional querystring of Users would match to an optional array parameter in the action method.
What would be the best way going about setting these up? A custom ActionFilterAttribute? Two different action methods? Multiple route entries in my routescollection?
Any info would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将定义以下路线:
然后为字符串数组编写自定义模型绑定程序:
最后我将像这样定义控制器操作:
如果您希望此自定义模型绑定程序应用于具有字符串数组的所有操作,您可以在
Application_Start
: 中声明操作参数:然后您的控制器操作将简单地变为:
I would define the following route:
and then write a custom model binder for a string array:
finally I would have the controller action defined like this:
and if you wanted this custom model binder to apply to all actions that have an array of string as action argument you could declare it in
Application_Start
:and then your controller action will simply become:
我最终创建了一个自定义 actionfilterattribute,它从请求中获取查询字符串 Users,并将其转换为长整型列表,然后将其放入操作参数中。 500 的参数在路由和操作方法中都被简单地设置为可选。
I ended up creating a custom actionfilterattribute that took the querystring Users from the request and converted it into a list of longs which i then placed into an actionparameter. The parameter for 500 was simply set to optional in both the route and the actionmethod.