ASP.Net MVC:需要特定路由的指南

时间:2018-02-02 10:01:05

标签: asp.net-mvc asp.net-mvc-routing

我的路由代码无效。

我以表格格式显示数据,包括排序和分页。我的解决方案正在运作当我将鼠标悬停在列上时,网址看起来像http://localhost:55831/Customers?page=2&SortColumn=CompanyName&CurrentSort=ContactName

当我点击分页数字链接时,网址看起来像http://localhost:55831/Customers?page=3&SortColumn=ContactName

我希望我的网址看起来像 1)http://localhost:55831/Customers/2/CompanyName/ContactName 2)http://localhost:55831/Customers/3/ContactName

所以我添加一个路由代码。这是

    routes.MapRoute(
        name: null,
        url: "Customers/{page}/{SortColumn}/{CurrentSort}",
        defaults: new
        {
            action = "Index",
            page = UrlParameter.Optional,
            SortColumn = UrlParameter.Optional,
            CurrentSort = UrlParameter.Optional
        }
    );

添加上面的路由代码后,url看起来有点怪异。现在网址看起来像

http://localhost:55831/Customers/1/CompanyName/CompanyName?controller=Customers
http://localhost:55831/Customers/2/CompanyName?controller=Customers

所以当我点击上面的链接然后我没有重定向到正确的控制器和操作而不是得到错误。

所以这意味着代码中存在一些问题,我在route.config.cs文件中添加了路由。

所以请帮助我得到我上面提到的我想要的网址。感谢

修改

我的完整路由代码

    routes.MapRoute(
        name: null,
        url: "{controller}/{action}/{page}/{SortColumn}/{CurrentSort}",
        defaults: new
        {
            action = "Index",
            page = UrlParameter.Optional,
            SortColumn = UrlParameter.Optional,
            CurrentSort = UrlParameter.Optional
        }
    );

    routes.MapRoute(
        name: null,
        url: "{controller}/{action}/{page}/{id}",
        defaults: new
        {
            controller = "Home",
            action = "Index",
            id = UrlParameter.Optional,
            page = UrlParameter.Optional,
        }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

1 个答案:

答案 0 :(得分:2)

这不起作用,因为您的路线具有与其他路线冲突的可选参数。

例如,请使用网址/MyController/MyAction/MyPage/213

路由如何确定您是否需要:{controller}/{action}/{page}/{id}{controller}/{action}/{page}/{SortColumn}/{CurrentSort}

一种解决方案可能是删除可选参数:

page = UrlParameter.Optional,
SortColumn = UrlParameter.Optional,
CurrentSort = UrlParameter.Optional

所以你的路线看起来更像是这样:

routes.MapRoute(
    name: "PageWithSort",
    url: "{controller}/{action}/{page}/{SortColumn}/{CurrentSort}",
    defaults: new { action = "Index" }
);

routes.MapRoute(
    name: "PageWithId",
    url: "{controller}/{action}/{page}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

但是,在这里,您仍然会与PageWithIdDefault路线发生冲突。因此,如果您从未在默认路由中使用id,那么您可能希望在那里删除可选参数。或者,如果page的所有路由始终都有id,那么您可以删除那里的可选参数。或者,如果pageid是真正可选的,您可以考虑将默认路由删除为PageWithId,两个选项将同时处理这两个路径。

旁注:您应该始终为路线命名,以便稍后可以执行以下操作:

@Html.RouteLink("Link Text", "RouteName", new { controller = "xxx" })

从您提供的代码中,您可以考虑更改

@Html.ActionLink("Company", "Index", new { page = ViewBag.CurrentPage, SortColumn = "CompanyName", CurrentSort = ViewBag.CurrentSort })

@Html.RouteLink("Company", "PageWithSort", new { controller = "Customers", action = "Index", page = ViewBag.CurrentPage, SortColumn = "CompanyName", CurrentSort = ViewBag.CurrentSort })