ActionLink to show parameters in URL instead of querystring?

前端 未结 2 1542
情书的邮戳
情书的邮戳 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")
                           }
                       );
    

提交回复
热议问题