Creating a RouteData instance from a URL

This post is a temporary placeholder for the real blog post on how to get a RouteData instance from an arbitrary URL. This post got a lot of traffic from StackOverflow and I’m in the process of changing my website around (a lot). I’m just putting the relevant code here for the benefit of folks who are coming from SO and will re-write this post shortly to contain a full description of the implementation.

RouteInfo Class:

    public class RouteInfo
    {
        public RouteData RouteData { get; private set; }

        public RouteInfo(RouteData data)
        {
            RouteData = data;
        }

        public RouteInfo(Uri uri, string applicationPath)
        {
            RouteData = RouteTable.Routes.GetRouteData(new InternalHttpContext(uri, applicationPath));
        }

        private class InternalHttpContext : HttpContextBase
        {
            private readonly HttpRequestBase _request;

            public InternalHttpContext(Uri uri, string applicationPath)
            {
                _request = new InternalRequestContext(uri, applicationPath);
            }

            public override HttpRequestBase Request { get { return _request; } }
        }

        private class InternalRequestContext : HttpRequestBase
        {
            private readonly string _appRelativePath;
            private readonly string _pathInfo;

            public InternalRequestContext(Uri uri, string applicationPath)
            {
                _pathInfo = uri.Query;

                if (String.IsNullOrEmpty(applicationPath) || !uri.AbsolutePath.StartsWith(applicationPath, StringComparison.OrdinalIgnoreCase))
                    _appRelativePath = uri.AbsolutePath.Substring(applicationPath.Length);
                else
                    _appRelativePath = uri.AbsolutePath;
            }

            public override string AppRelativeCurrentExecutionFilePath { get { return String.Concat("~", _appRelativePath); } }
            public override string PathInfo { get { return _pathInfo; } }
        }
    }

Usage:

    Uri uri = new Uri("http://www.yoursite.com/somepage");
    RouteInfo routeInfo = new RouteInfo(uri, HttpContext.Current.Request.ApplicationPath);

    // Here it is...
    RouteData routeData = routeInfo.RouteData;

Useful extension methods:

        public static bool IsRouteMatch(this Uri uri, string controllerName, string actionName)
        {
            RouteInfo routeInfo = new RouteInfo(uri, HttpContext.Current.Request.ApplicationPath);
            return (routeInfo.RouteData.Values["controller"].ToString() == controllerName && routeInfo.RouteData.Values["action"].ToString() == actionName);
        }

        public static string GetRouteParameterValue(this Uri uri, string paramaterName)
        {
            RouteInfo routeInfo = new RouteInfo(uri, HttpContext.Current.Request.ApplicationPath);
            return routeInfo.RouteData.Values[paramaterName] != null ? routeInfo.RouteData.Values[paramaterName].ToString() : null;
        }
Posted in C#, MVC
0 comments on “Creating a RouteData instance from a URL
1 Pings/Trackbacks for "Creating a RouteData instance from a URL"
  1. [...] Having implemented the following solution to deal with one of my routing issues Creating a routedata instance from a url [...]