具有可选 URL 段的 ASP.NET 路由
我正在处理 ASP.NET MVC 任务列表,我想在过滤列表时了解 URL 路由。我有一个如下定义的操作方法:
public ActionResult List(int categoryID, bool showCompleted, TaskFilter filter);
enum TaskFilter { MyTasks, MyDepartmentTasks, AllTasks }
我希望我的 URL 看起来像这样:
/Tasks/Category4/MyTasks/ShowCompleted/
/Tasks/Category4/MyDepartment
/Tasks/Category4/
Category#
段将始终存在。我希望 MyTasks|MyDepartment|AllTasks
段是可选的,如果不存在则默认为 AllTasks
。我还希望 ShowCompleted 是可选的,默认为 false。
这种路由是否可行,或者我是否必须回退并仅使用查询字符串参数?
后续/额外学分问题:如果我还想要操作方法上的第四个参数来按任务截止日期进行过滤,看起来像 Today|Day2Through10
(如果不存在,则默认为 Today
) ?
I'm working on an ASP.NET MVC task list and I'd like to get fancy with the URL routing when filtering the list. I have an action method defined like this:
public ActionResult List(int categoryID, bool showCompleted, TaskFilter filter);
enum TaskFilter { MyTasks, MyDepartmentTasks, AllTasks }
I want my URLs to look like this:
/Tasks/Category4/MyTasks/ShowCompleted/
/Tasks/Category4/MyDepartment
/Tasks/Category4/
The Category#
segment will always be present. I'd like the MyTasks|MyDepartment|AllTasks
segment to be optional, defaulting to AllTasks
if absent. I'd also like ShowCompleted
to be optional, defaulting to false.
Is this sort of routing possible, or am I going to have to fall back and just use querystring parameters?
Followup/extra credit question: What if I also wanted a fourth parameter on the action method to filter by task due date that looked like Today|Day2Through10
(default Today
if absent)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下内容涵盖了您的第一个问题,但略有更改:
您需要将 List 方法更改为如下所示:
对于第二个查询,您只需使用 {Day2} 等即可传递给 ActionResult。从我给你的内容中你应该能够弄清楚。
The following covers your first question with a slight change:
You will need to change the List method to start like the following:
For your second query, you just need to use {Day2} and such to be passed to the ActionResult. You should be able to figure it out from what I've given you.
查看 MvcContrib 库。下面是添加带有约束的路由的流畅界面的示例: http://www.codinginstinct.com/2008/09/url-routing-available-in-mvccontrib.html
Look into the MvcContrib library. Here's an example of the fluent interface of adding routes with constraints: http://www.codinginstinct.com/2008/09/url-routing-available-in-mvccontrib.html