Routing Reserved Words in ASP.Net

前端 未结 2 1546
一个人的身影
一个人的身影 2020-12-12 02:23

I have a legacy url that I wish to map to a route in my ASP.Net MVC application

e.g. http://my.domain.com/article/?action=detail&item=22
<
2条回答
  •  猫巷女王i
    2020-12-12 03:02

    controller,action and area are the only reserved words in asp.net MVC. "Reserved" means that MVC gives special meaning to them, especially for Routing.

    There are also others words (COM1-9, LPT1-9, AUX, PRT, NUL, CON), not specific to asp.net, than can not be in the url. This is explained why here and how to by-pass here.

    Edit : There are no ways to use them because asp.net mvc relies on them in route data.

    Here is an decompiled example taken from UrlHelper :

    // System.Web.Mvc.RouteValuesHelpers
    public static RouteValueDictionary MergeRouteValues(string actionName, string controllerName, RouteValueDictionary implicitRouteValues, RouteValueDictionary routeValues, bool includeImplicitMvcValues)
    {
        RouteValueDictionary routeValueDictionary = new RouteValueDictionary();
        if (includeImplicitMvcValues)
        {
            object value;
            if (implicitRouteValues != null && implicitRouteValues.TryGetValue("action", out value))
            {
                routeValueDictionary["action"] = value;
            }
            if (implicitRouteValues != null && implicitRouteValues.TryGetValue("controller", out value))
            {
                routeValueDictionary["controller"] = value;
            }
        }
        if (routeValues != null)
        {
            foreach (KeyValuePair current in RouteValuesHelpers.GetRouteValues(routeValues))
            {
                routeValueDictionary[current.Key] = current.Value;
            }
        }
        if (actionName != null)
        {
            routeValueDictionary["action"] = actionName;
        }
        if (controllerName != null)
        {
            routeValueDictionary["controller"] = controllerName;
        }
        return routeValueDictionary;
    }
    

提交回复
热议问题