How do you override route table 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 is my actionlink

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

When the filter level is specified in the actionlink, it generates a URL similar to this:

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

This is the same page I'm on now. If I change the actionlink to use a property other than the default value in the route table (yes, if I use filterDate, it will go bad 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 right? Should I not override the default settings set in the route table? I confirmed that if I remove the default filterLevel value from the route table, this works the way 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);
        }

, , global.asax. filterLevel filterDate.

+3
4

SLaks , , , . , , , ( global.asax)?

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

, , , SLaks, , . , , , , . , , . filterLevel filterDate . querystring Html.ActionLink().

, :

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

:

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);
    }
}
+3

, , URL-, , - URL-, - .

.

.

+1

, :

public ActionResult Machine(string id, DateTime? filterDate = null, string filterLevel = "INFO")
{
    filterDate = filterDate ?? DateTime.Now.AddDays(-1);  
    var model = new LogListViewModel { LogEntries = logEntries };
    return View(model);
}
+1

, URL, , URL.

. , .

routes.MapRoute(
            "Default1", // Route name
            "foo/{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional
            , filterLevel = "INFO" } // Parameter defaults
        );

routes.MapRoute(
            "Default2", // Route name
            "bar/{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional
            , filterLevel = "DEBUG" } // Parameter defaults
        );

, filterLevel , URL "{filterLevel}".

URL- , ?

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

If you can override the default value for filterLevel, you can expect both routes to match. But that doesn't make sense. In this case, not a single match, because filterLevel is not in the URL pattern, and therefore the supplied filterLevel must match the default value. So you can do this:

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

//AND

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

to create the URLs for the first and second routes, respectively.

This confusion is why I always recommend always using named routes .

+1
source

Source: https://habr.com/ru/post/1789284/


All Articles