ASP.Net MVC5:路由中的额外段导致问题

时间:2018-02-09 13:31:00

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

处理我的路由问题我在路由中添加了额外的段,而不是将我重定向到编辑操作。

查看导致问题的路由

routes.MapRoute
(
    name: "PageWithId",
    url: "Customers/Action/Edit/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit" }
);

OR

routes.MapRoute
(
    name: "PageWithId",
    url: "Customers/Edit/Action/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit" }
);

我为PageWithId测试了2个不同的路由集但没有工作

请参阅RouteLink代码

@Html.RouteLink("Edit", "PageWithId",
new
{
        controller = "Customers",
        action = "Edit",
        id = item.CustomerID,
        page = ViewBag.CurrentPage
})

我的编辑操作代码

public ActionResult Edit(int page, string id)
{
    if (id == null)
    {
    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Customer customer = db.Customers.Find(id);
    if (customer == null)
    {
    return HttpNotFound();
    }
    ViewBag.CurrentPage = page;
    return View(customer);
}  

现在告诉为什么这个网址http://localhost:2020/Customers/Action/Edit/1/AlFAKI没有重定向我进行编辑操作?

查看我的完整路由代码

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


routes.MapRoute
(
    name: "PageWithId",
    url: "Customers/Action/Edit/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit" }
);

OR

routes.MapRoute
(
    name: "PageWithId",
    url: "Customers/Edit/Action/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit" }
);

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

1 个答案:

答案 0 :(得分:0)

你的路线看起来不对,我相信你要做的是:

routes.MapRoute(
    name: "PageWithId",
    url: "{controller}/{action}/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit" }
);

您的网址将是:http://localhost:2020/Customers/Edit/1/AlFAKI