ActionLink to show parameters in URL instead of querystring?

前端 未结 2 1540
情书的邮戳
情书的邮戳 2020-12-09 20:38

I have this route defined:

routes.MapRoute(
                   \"Details\", // Route name
                   \"{home}/{details}/{id}/{name}\", // URL wit         


        
2条回答
  •  北海茫月
    2020-12-09 21:28

    Your route definition should be like this:

    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
    );
    

    Also you should use the proper overload:

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

    Notice the null at the end.

提交回复
热议问题