Search code examples
asp.netasp.net-mvc-3razorasp.net-mvc-routingactionlink

How do you override route table default values using Html.ActionLink?


Global.asax route values

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional, filterDate = DateTime.Now.AddDays(-1), filterLevel = "INFO" } // Parameter defaults
        );

Here's my actionlink

        @Html.ActionLink(item.MachineName, "Machine", new { id = item.MachineName, filterLevel = "hello" }, null)

When the filterlevel is specified in the actionlink, it generates a url like this:

http://localhost:1781/LoggingDashboard/log/level/VERBOSE

Which is the same page as I am currently on. If I change the actionlink to use a property other than one that has a default value in the route table (yes, if I use filterDate it messes up too), it generates a link like this:

@Html.ActionLink(item.MachineName, "Machine", new { id = item.MachineName, foo = "bar" }, null)

http://localhost:1781/LoggingDashboard/log/Machine/C0UPSMON1?foo=bar

Is this behavior correct? Should I not be able to override the defaults set up in the route table? I have confirmed that if I remove the filterLevel default from the route table this works as I expect:

http://localhost:1781/LoggingDashboard/log/Machine/C0UPSMON1?filterLevel=VERBOSE

---EDIT--- sorry, here is the action

        public ActionResult Machine(string id, DateTime filterDate, string filterLevel)
        {
...
            var model = new LogListViewModel { LogEntries = logEntries };
            return View(model);
        }

For the bounty I want to know how to override the "default" values that are specified in the routes from global.asax. i.e. I want to be able to override filterLevel and filterDate.


Solution

  • SLaks already said what is probably the best way to handle this problem. I don't know if this will work, but, what happens if you put this above the existing route (so there would be two in your global.asax now)?

            routes.MapRoute(
                "Filtered",
                "{controller}/{action}/{id}?filterLevel={filterLevel}&filterDate={filterDate}",
                new
                    {
                        controller = "Home",
                        action = "Index",
                        id = UrlParameter.Optional,
                        filterDate = DateTime.Now.AddDays(-1),
                        filterLevel = "INFO"
                    } 
                );
    

    Also, it just occurred to me that the reason you don't like SLaks' solution is that it could be repetitive. Since you only have one route, these parameters probably indicate a global functionality, instead of an action-scoped functionality. You could fix this by adding the values in an action filter on each controller, or your could use a custom route handler to apply this globally. Either of these would allow you to take the filterLevel and filterDate fields out of your route definition and still get the scope you want. Then it should be no problem to pass the parameters in a querystring with Html.ActionLink().

    To do this with the route handler, change your route definition to:

            routes.Add(
                new Route(
                        "{controller}/{action}/{id}", 
                        new RouteValueDictionary(new{ controller = "Home", action = "Index", id = UrlParameter.Optional}), 
                        new CustomRouteHandler()));
    

    Your implementation of the route handler would be something like this:

    public class CustomRouteHandler : IRouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var routeValues =  requestContext.RouteData.Values;
            if(!routeValues.ContainsKey("filterLevel"))
            {
                routeValues.Add("filterLevel","INFO");
            }
            if(!routeValues.ContainsKey("filterDate"))
            {
                routeValues.Add("filterDate", DateTime.Now.AddDays(-1));
            }
            var mvcRouteHandler = new MvcRouteHandler(); 
            return (mvcRouteHandler as IRouteHandler).GetHttpHandler(requestContext);
        }
    }