MVC RouteLink URL格式

时间:2017-04-07 09:03:24

标签: c# asp.net-mvc

我有以下RouteLink

@Html.RouteLink("Accept Offer", new { controller = "Case", action = "Accept", id = item.CaseId, offerid = item.PxOfferId }, new { @class = "btn btn-success" })

将URL格式化为:

http://localhost:54644/Clients/Case/Accept/15847?offerid=3103

如何将网址格式化为:

http://localhost:54644/Clients/Case/Accept/15847/3103

感谢。

默认路线:

    context.MapRoute(
        "Clients_default",
        "Clients/{controller}/{action}/{id}",
        new { controller = "Login", action = "Index", id = UrlParameter.Optional }
    );

如果我把它放在我定义的路线中,那么路线是可行的:

    context.MapRoute(
        name: "Accept Offer",
        url: "Clients/{controller}/{action}/{id}/{offerid}",
        defaults: new { controller = "Case", action = "Accept", id = 0, offerid = UrlParameter.Optional }
    );

然后在其他页面上导致错误。

1 个答案:

答案 0 :(得分:1)

您在RouteConfig的{​​{1}}文件中找到了一条路由(如果您使用的是mvc3 / 4,则可以在App_Start文件中配置路由 ):

Global.asax

用法:

routes.MapRoute(
    name: "MyCaseRoute",
    url: "/clients/{controller}/{action}/{id}/{offerId}",
    defaults: new { controller = "Case", action = "Accept", id = UrlParameter.Optional, offerId = UrlParameter.Optional }
);