Beispiel #1
0
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            // Invoke the internal RouteParser class via reflection to create a ParsedRoute instance from the route's URL template.
            var parsedRoute = new RouteParserReflectionProxy().Parse(Url);

            // Build the full requested path
            var requestedVirtualPath = string.Format(
                "{0}{1}",
                httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2),
                httpContext.Request.PathInfo);

            // Parse the requested url into a RouteValueDictionary, including the route's defaults
            var routeDataValues = parsedRoute.Match(requestedVirtualPath, Defaults);

            if (routeDataValues == null)
            {
                return(null);
            }

            // Apply customizaed ADNSF behavior
            ApplyQueryStringToRouteDataValues(httpContext.Request.QueryString, routeDataValues);
            ApplyRouteDataValueTransformations(routeDataValues, RouteDirection.IncomingRequest);

            // Validate all constraints are met
            if (Constraints != null)
            {
                foreach (var constraint in Constraints)
                {
                    var constraintResult = ProcessConstraint(httpContext, constraint.Value, constraint.Key, routeDataValues, RouteDirection.IncomingRequest);
                    if (!constraintResult)
                    {
                        return(null);
                    }
                }
            }

            // Prepare the RouteData object to return
            var routeData = new RouteData(this, RouteHandler);

            // Copy in the route data values, including the query string includes and transformations
            foreach (var routeDataEntry in routeDataValues)
            {
                routeData.Values.Add(routeDataEntry.Key, routeDataEntry.Value);
            }

            // Copy in any data tokens
            if (DataTokens != null)
            {
                foreach (var dataToken in DataTokens)
                {
                    routeData.DataTokens.Add(dataToken.Key, dataToken.Value);
                }
            }

            return(routeData);
        }
Beispiel #2
0
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            // We need to modify the route data values for purposes of generating a URL, but we don't want to modify the collection
            // that's passed in. We'll clone it and modify the clone.
            var routeDataValues = new RouteValueDictionary(values);

            // Apply customizaed ADNSF behavior
            ApplyQueryStringToRouteDataValues(requestContext.HttpContext.Request.QueryString, routeDataValues);
            ApplyRouteDataValueTransformations(routeDataValues, RouteDirection.UrlGeneration);

            // Invoke the internal RouteParser class via reflection to create a ParsedRoute instance from the route's URL template.
            var parsedRoute = new RouteParserReflectionProxy().Parse(Url);

            // Invoke the internal Bind method to create a URL and new route data values from the provided route data.
            var boundUrl = parsedRoute.Bind(requestContext.RouteData.Values, routeDataValues, Defaults, Constraints);

            if (boundUrl == null)
            {
                return(null);
            }

            // Run the constraints against the new route data values
            if (Constraints != null)
            {
                foreach (var constraint in Constraints)
                {
                    var constraintResult = ProcessConstraint(requestContext.HttpContext, constraint.Value, constraint.Key, boundUrl.Values, RouteDirection.UrlGeneration);
                    if (!constraintResult)
                    {
                        return(null);
                    }
                }
            }

            // Construct the return value from the bound URL and this route
            var virtualPathData = new VirtualPathData(this, boundUrl.Url);

            // Copy in any data tokens
            if (DataTokens != null)
            {
                foreach (var dataToken in DataTokens)
                {
                    virtualPathData.DataTokens[dataToken.Key] = dataToken.Value;
                }
            }

            return(virtualPathData);
        }