ActionLink在URL中显示参数而不是查询字符串?

Dan*_*nny 10 asp.net-mvc asp.net-mvc-3

我定义了这条路线:

routes.MapRoute(
                   "Details", // Route name
                   "{home}/{details}/{id}/{name}", // URL with parameters
                   new
                   {
                       controller = "Home",
                       action = "Details",
                       id = UrlParameter.Optional,
                       name = UrlParameter.Optional
                   } // Parameter defaults
               );
Run Code Online (Sandbox Code Playgroud)

ActionLink:

 @Html.ActionLink("Show Details", "Details", "MyController", new { id = 1, name ="a" })
Run Code Online (Sandbox Code Playgroud)

动作链接导致/Home/Details/1?name=a我追随/Home/List/1/a

Dar*_*rov 12

您的路线定义应如下所示:

routes.MapRoute(
    "Details", // Route name
    "{controller}/{action}/{id}/{name}", // URL with parameters
    new
    {
        controller = "Home",
        action = "Details",
        id = UrlParameter.Optional,
        name = UrlParameter.Optional
    } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)

你也应该使用适当的过载:

@Html.ActionLink(
    "Show Details",             // linkText
    "Details",                  // action
    "MyController",             // controller
    new { id = 1, name = "a" }, // routeValues
    null                        // htmlAttributes
)
Run Code Online (Sandbox Code Playgroud)

注意到null最后.