MVC 2 路由显示为参数

发布于 2024-09-10 22:11:14 字数 4275 浏览 8 评论 0原文

我正在尝试建立一条格式为的路线 {controller}{action}{classificationID}{reviewID}{CategoryID}\

但我希望能够在没有最终 CategoryID 的情况下解决它,并在未给出的情况下将默认值设置为 1。

这是我的路线:

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

        routes.MapRoute(
            "SchoolRouteNoCategory", // Route name
            "{controller}/{action}/{ClassificationID}/{ReviewID}/", // URL with parameters
            new { controller = "School", action = "Edit", ClassificationID = "SBP", ReviewID = "SAR" } // Parameter defaults
        );

        routes.MapRoute(
            "SchoolRoute", // Route name
            "{controller}/{action}/{SchoolID}/{ReviewID}/{CategoryID}/", // URL with parameters
            new RouteValueDictionary { { "action", "index" }, { "SchoolID", UrlParameter.Optional }, { "ReviewID", UrlParameter.Optional }, { "CategoryID", UrlParameter.Optional } } // Parameter defaults
        );

这是我的编辑操作的控制器签名:

    public ActionResult Edit(string SchoolID, string ReviewID) {}

    public ActionResult Edit(string SchoolID, string ReviewID, int CategoryID) {}

第一个编辑操作将仅添加默认的 CategoryID 并将其转发到第二个编辑操作。

我的问题是,当我尝试在索引视图中构建 ActionLink 时,我得到以下 URL:

http://localhost:21271/School/Edit?ClassificationID=SBP&ReviewID=SAR

其中我应该有

http://localhost:21271/School/Edit/SBP/SAR/

ActionLink 的代码是

<%: Html.ActionLink("Edit", "Edit", new { item.ClassificationID, item.ReviewID}) %>

我开始感觉我对路由的了解比我还少我想,但我能找到的大多数教程只关注非常简单的路线,而不涵盖任何复杂的内容。是否有任何教程涵盖了我想要做的路由类型?

编辑:

遇到了一个新的路由问题,我想我会尝试先将其发布到此处,而不是创建一个新线程。

我的编辑页面上有以下 ActionLink: ,

<%: Html.ActionLink(Model.Categories[i].name, "Edit", new { AcadPeriod = Model.AcadPeriod, ClassificationID=Model.ClassificationID, ReviewID=Model.ReviewID, CategoryID=Model.Categories[i].category_id })%>

这会产生以下 url:

http://localhost:21271/School/Edit?ClassificationID=SBP&ReviewID=SAR&CategoryID=2

如果我运行路由调试器,我会得到以下输出。

Matched Route: {controller}/{action}/{AcadPeriod}/{id}
Generated URL: /School/Edit/1011/SBP/SAR/2 using the route "{controller}/{action}/{AcadPeriod}/{ClassificationID}/{ReviewID}/{CategoryID}"

但是,在路由表中,它说 {controller}/{action}/{AcadPeriod}/{ClassificationID}/{ReviewID}/{CategoryID} 不匹配?

它似乎在同一页上告诉我两件不同的事情。

这是我目前在 global.asax 中的路线:

        routes.MapRoute(
            "SchoolRoute1",
            "{controller}/{action}/{AcadPeriod}/{ClassificationID}/{ReviewID}/{CategoryID}",
            new { controller = "School", action = "Edit", AcadPeriod = "1011", CategoryID = "1" }
        );

        routes.MapRoute(
            "SchoolRouteNoCategory",
            "{controller}/{action}/{AcadPeriod}/{ClassificationID}/{ReviewID}",
            new { controller = "School", action = "Edit", AcadPeriod = "1011", CategoryID = "1" }
        );

        routes.MapRoute(
            "SchoolRoute",
            "{controller}/{action}/{ClassificationID}/{ReviewID}/{CategoryID}",
            new { controller = "School", action = "Edit", CategoryID = "1" }
        );


        routes.MapRoute(
            "SchoolIndex", // Route name
            "{controller}/{action}/{AcadPeriod}/{id}", // URL with parameters
            new { controller = "School", action = "Index", AcadPeriod = "1011", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "SchoolIndex1", // Route name
            "{controller}/{action}/{AcadPeriod}", // URL with parameters
            new { controller = "School", action = "Index", AcadPeriod = "1011" } // Parameter defaults
        );

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

有什么想法吗?

I am trying to build a route with the format of
{controller}{action}{classificationID}{reviewID}{CategoryID}\

but I want to be able to address it without the final CategoryID and have that default to 1 where it isn't given.

Here are my routes:

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

        routes.MapRoute(
            "SchoolRouteNoCategory", // Route name
            "{controller}/{action}/{ClassificationID}/{ReviewID}/", // URL with parameters
            new { controller = "School", action = "Edit", ClassificationID = "SBP", ReviewID = "SAR" } // Parameter defaults
        );

        routes.MapRoute(
            "SchoolRoute", // Route name
            "{controller}/{action}/{SchoolID}/{ReviewID}/{CategoryID}/", // URL with parameters
            new RouteValueDictionary { { "action", "index" }, { "SchoolID", UrlParameter.Optional }, { "ReviewID", UrlParameter.Optional }, { "CategoryID", UrlParameter.Optional } } // Parameter defaults
        );

and here are my controller signatures for the edit action:

    public ActionResult Edit(string SchoolID, string ReviewID) {}

    public ActionResult Edit(string SchoolID, string ReviewID, int CategoryID) {}

The first Edit Action will just add the default CategoryID and forward it to the second Edit Action.

My problem is that when i try to build the ActionLink in my Index view, I get the following URL:

http://localhost:21271/School/Edit?ClassificationID=SBP&ReviewID=SAR

where as I should have

http://localhost:21271/School/Edit/SBP/SAR/

The code for the ActionLink is

<%: Html.ActionLink("Edit", "Edit", new { item.ClassificationID, item.ReviewID}) %>

I'm starting to get the feeling that I know even less about routing than I thought, but most of the tutorials I can find only focus on very simply routes and don't cover anything complicated. Are there any tutorials that cover the sort of routing I am trying to do?

EDIT:

Got a new routing issue, thought I'd try posting it here first rather than creating a new thread.

I have the following ActionLink on my edit page:

<%: Html.ActionLink(Model.Categories[i].name, "Edit", new { AcadPeriod = Model.AcadPeriod, ClassificationID=Model.ClassificationID, ReviewID=Model.ReviewID, CategoryID=Model.Categories[i].category_id })%>

which results in the following url:

http://localhost:21271/School/Edit?ClassificationID=SBP&ReviewID=SAR&CategoryID=2

If I run the route debugger, I get the following output.

Matched Route: {controller}/{action}/{AcadPeriod}/{id}
Generated URL: /School/Edit/1011/SBP/SAR/2 using the route "{controller}/{action}/{AcadPeriod}/{ClassificationID}/{ReviewID}/{CategoryID}"

However, in the routes table, it says that {controller}/{action}/{AcadPeriod}/{ClassificationID}/{ReviewID}/{CategoryID} doesn't match?

It seems to be telling me two different things on the same page.

This is my routes in global.asax as they currently stand:

        routes.MapRoute(
            "SchoolRoute1",
            "{controller}/{action}/{AcadPeriod}/{ClassificationID}/{ReviewID}/{CategoryID}",
            new { controller = "School", action = "Edit", AcadPeriod = "1011", CategoryID = "1" }
        );

        routes.MapRoute(
            "SchoolRouteNoCategory",
            "{controller}/{action}/{AcadPeriod}/{ClassificationID}/{ReviewID}",
            new { controller = "School", action = "Edit", AcadPeriod = "1011", CategoryID = "1" }
        );

        routes.MapRoute(
            "SchoolRoute",
            "{controller}/{action}/{ClassificationID}/{ReviewID}/{CategoryID}",
            new { controller = "School", action = "Edit", CategoryID = "1" }
        );


        routes.MapRoute(
            "SchoolIndex", // Route name
            "{controller}/{action}/{AcadPeriod}/{id}", // URL with parameters
            new { controller = "School", action = "Index", AcadPeriod = "1011", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "SchoolIndex1", // Route name
            "{controller}/{action}/{AcadPeriod}", // URL with parameters
            new { controller = "School", action = "Index", AcadPeriod = "1011" } // Parameter defaults
        );

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

Any ideas?

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

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

发布评论

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

评论(2

一念一轮回 2024-09-17 22:11:14

首先,按照从上到下的顺序评估路由。您列出的第一条路线将匹配任何内容,因此您的其他路线将永远不会被考虑。

这意味着最具体的路线应该位于顶部。默认的“包罗万象”路线应该位于底部。

我认为您想要的路线是(第二条和第三条路线应该相关吗?):

routes.MapRoute(
    "SchoolRoute",
    "{controller}/{action}/{SchoolID}/{ReviewID}/{CategoryID}",
    new { controller = "School", action = "Edit", CategoryID = "1" }
);

如果它不是可选的并且没有默认值,则无需将其列出在路线声明的第三行中。

Firstly, routes are evaluated in order from top to bottom. The first route that you have listed will match anything so your additional routes will never even be considered.

This means that the most specific routes should go at the top. And the default, "catch-all" route should go at the bottom.

I think the route you want is (are the second & third route supposed to be related?):

routes.MapRoute(
    "SchoolRoute",
    "{controller}/{action}/{SchoolID}/{ReviewID}/{CategoryID}",
    new { controller = "School", action = "Edit", CategoryID = "1" }
);

If it's not optional and there's no default value, there's no need to list it in the third line of the route declaration.

请远离我 2024-09-17 22:11:14

您需要将自定义路由放在默认操作/控制器/id 路由之上。按照与您所拥有的顺序相反的顺序列出它们。

You need to put your custom routes above the default action/controller/id route. List them in the opposite order of what u have.

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