ASP.NET MVC 3 - 难以理解路由

发布于 2024-12-26 15:28:34 字数 1112 浏览 0 评论 0原文

我使用 NerdDinner 教程作为工作基础,在 MVC 3 中创建了一个系统。我不确定我是否完全理解路由。

一切都工作正常,直到我向我拥有的分页助手添加了一种排序。

这是 global.asax.cs,

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "UpcomingKeyDates", // Route name
        "KeyDates.mvc/{sortBy}/Page/{page}", // URL with parameters
        new { controller = "Home", action = "Index" } // Parameter defaults
    );

    routes.MapRoute(
       "Default", // Route name
       "{controller}.mvc/{action}/{id}", // URL with parameters
       new { controller = "Home", action = "Index", sortBy = "EventDate" } // Parameter defaults
    );

    routes.MapRoute(
        "Root", // Route name
        "", // URL with parameters
        new { controller = "Home", action = "Index", sortBy = "EventDate" } // Parameter defaults
    );

}

当您第一次导航到该页面时,我希望默认列表按事件日期升序排序(效果很好)。排序和分页也很好用。但是,当我使用此链接时...

<%: Html.ActionLink("Create New", "Create", "Home") %>

该链接仅指向同一页面。我需要添加新路线或修改现有路线吗?非常感谢任何帮助。

谢谢。

I've created a system in MVC 3 using the NerdDinner tutorial as a base to work off. I'm not sure I fully understand Routing.

Everything was working fine until I added a sort to the Pagination helper that I have.

Here is the global.asax.cs

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "UpcomingKeyDates", // Route name
        "KeyDates.mvc/{sortBy}/Page/{page}", // URL with parameters
        new { controller = "Home", action = "Index" } // Parameter defaults
    );

    routes.MapRoute(
       "Default", // Route name
       "{controller}.mvc/{action}/{id}", // URL with parameters
       new { controller = "Home", action = "Index", sortBy = "EventDate" } // Parameter defaults
    );

    routes.MapRoute(
        "Root", // Route name
        "", // URL with parameters
        new { controller = "Home", action = "Index", sortBy = "EventDate" } // Parameter defaults
    );

}

I want to default the list to sort by Event Date ascending when you first navigate to the page (which works fine). The sort and pagination also works fine. However, when I'm using this link...

<%: Html.ActionLink("Create New", "Create", "Home") %>

The link just directs to the same page. Do I need to add a new route, or amend an existing route? Any help much appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

躲猫猫 2025-01-02 15:28:34

默认路由应始终出现在最后,并且是包罗万象的路由。它将自动捕获空路由,相当于 http://yourdomain.com/

默认路由应始终具有以下格式

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "foo", action = "bar", id=UrlParameter.Optional }
);

另外,如果页面将是数字,您可以限制它使用正则表达式(见下文)。

简而言之,更改您的 Global.asax ,使其看起来像这样:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "UpcomingKeyDates", // Route name
        "KeyDates.mvc/{sortBy}/Page/{page}", // URL with parameters
        new { controller = "Home", action = "Index" }, // Parameter defaults
        new { page = @"\d+" } // Note I have constrained the page so it has to be an integer...
    );

    routes.MapRoute(
       "MyDefaultRoute", // Your special default which inserts .mvc into every route
       "{controller}.mvc/{action}/{id}", // URL with parameters
       new { controller = "Home", action = "Index", id=UrlParameter.Optional, sortBy = "EventDate" } // Parameter defaults
    );

    routes.MapRoute(
       "Default", // Real default route. Matches any other route not already matched, including ""
       "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id=UrlParameter.Optional, sortBy = "EventDate" } // Parameter defaults
    );
}

The default route should always appear last and is the catch-all route. It will automatically catch the empty route which is equivalent to http://yourdomain.com/

The default route should always have the following format

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "foo", action = "bar", id=UrlParameter.Optional }
);

Also, if the page is going to be a number, you can constrain it using a regular expression (see below).

In brief, change your Global.asax so it looks like this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "UpcomingKeyDates", // Route name
        "KeyDates.mvc/{sortBy}/Page/{page}", // URL with parameters
        new { controller = "Home", action = "Index" }, // Parameter defaults
        new { page = @"\d+" } // Note I have constrained the page so it has to be an integer...
    );

    routes.MapRoute(
       "MyDefaultRoute", // Your special default which inserts .mvc into every route
       "{controller}.mvc/{action}/{id}", // URL with parameters
       new { controller = "Home", action = "Index", id=UrlParameter.Optional, sortBy = "EventDate" } // Parameter defaults
    );

    routes.MapRoute(
       "Default", // Real default route. Matches any other route not already matched, including ""
       "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id=UrlParameter.Optional, sortBy = "EventDate" } // Parameter defaults
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文