ActionLink to show parameters in URL instead of querystring?

前端 未结 2 1541
情书的邮戳
情书的邮戳 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:22

    A wild guess :

    probably your route was registered after the default route. Put it as first route inside your global.asax then it will work.

    Like below :

        public static void RegisterRoutes(RouteCollection routes) {
    
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute(
                               "Details", // Route name
                               //Put action instead of details
                               "{home}/{action}/{id}/{name}", // URL with parameters
                               new
                               {
                                   controller = "Home",
                                   action = "Details",
                                   id = UrlParameter.Optional,
                                   name = UrlParameter.Optional
                               } // Parameter defaults
                           );
    
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
    
        }
    

    UPDATE

    @Simon is correct, but you can use another way if you want.

    In order for the route to work only for one action method, use following code.

    Create a constraint as follows :

    public class EqualConstraint : IRouteConstraint {
    
        private string _match = String.Empty;
    
        public EqualConstraint(string match) {
    
            _match = match;
        }
    
        public bool Match(System.Web.HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
    
            return string.Equals(values[parameterName].ToString(), _match);
        }
    }
    

    And then change your route like below :

        routes.MapRoute(
                           "Details", // Route name
                           //Put action instead of details
                           "{home}/{action}/{id}/{name}", // URL with parameters
                           new
                           {
                               controller = "Home",
                               action = "Details",
                               id = UrlParameter.Optional,
                               name = UrlParameter.Optional
                           }, // Parameter defaults
                           new { 
                              controller = new EqualConstraint("Home"),
                              action = new EqualConstraint("Details")
                           }
                       );
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题