具有可选 URL 段的 ASP.NET 路由

发布于 2024-08-08 14:25:08 字数 690 浏览 7 评论 0原文

我正在处理 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|AllTask​​s 段是可选的,如果不存在则默认为 AllTask​​s 。我还希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海似她心 2024-08-15 14:25:08

以下内容涵盖了您的第一个问题,但略有更改:

routes.MapRoute(
    "t1",
    "Tasks/Category{categoryID}",
    new
    {
        controller = "Task",
        action = "List",
        showCompleted = false,
        strFilter = TaskFilter.AllTasks.ToString()
    }
    );

routes.MapRoute(
    "t2",
    "Tasks/Category{categoryID}/{strFilter}/",
    new
    {
        controller = "Task",
        action = "List",
        showCompleted = false
    }
);

routes.MapRoute(
    "t3",
    "Tasks/Category{categoryID}/{strFilter}/ShowCompleted",
    new { controller = "Task", action = "List", showCompleted = true }
    );

您需要将 List 方法更改为如下所示:

public ActionResult List(int categoryID, bool showCompleted, string strFilter)
{
    TaskFilter filter = (TaskFilter)Enum.Parse(typeof(TaskFilter), strFilter);

对于第二个查询,您只需使用 {Day2} 等即可传递给 ActionResult。从我给你的内容中你应该能够弄清楚。

The following covers your first question with a slight change:

routes.MapRoute(
    "t1",
    "Tasks/Category{categoryID}",
    new
    {
        controller = "Task",
        action = "List",
        showCompleted = false,
        strFilter = TaskFilter.AllTasks.ToString()
    }
    );

routes.MapRoute(
    "t2",
    "Tasks/Category{categoryID}/{strFilter}/",
    new
    {
        controller = "Task",
        action = "List",
        showCompleted = false
    }
);

routes.MapRoute(
    "t3",
    "Tasks/Category{categoryID}/{strFilter}/ShowCompleted",
    new { controller = "Task", action = "List", showCompleted = true }
    );

You will need to change the List method to start like the following:

public ActionResult List(int categoryID, bool showCompleted, string strFilter)
{
    TaskFilter filter = (TaskFilter)Enum.Parse(typeof(TaskFilter), strFilter);

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.

甜宝宝 2024-08-15 14:25:08

查看 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文