5

I have a String inside the action of one of my controllers that represents a Referring URL. The current request's route data is not what I'm looking for (because it is being called from the script tag inside another view).

I want to find the Action and Controller for the referring Url.

Is there some way can I manually use a string like "/Product/23" to find the controller and action the string as a url would produce?

ctrlShiftBryan
  • 25,858
  • 25
  • 70
  • 77

2 Answers2

8

I blogged about doing this exact thing a couple weeks ago:

Creating a RouteData instance from a URL

Scott
  • 13,523
  • 20
  • 89
  • 148
  • Thanks. Looks like they key is you have to create a concrete implementation HttpRequestBase. With that you turn a URI and Application Path into Route. – ctrlShiftBryan Feb 09 '11 at 19:28
  • @ctrl and @Scott - I should note that while that code will *mostly* work, there are cases where it won't. A custom route constraint can choose to do all sorts of crazy things and those crazy things might not work if you try to re-scan the route table. The part of routing is meant to execute only during the very early parts of the ASP.NET request pipeline, and meant to execute only once. I'm not saying you shouldn't do it, but I'm warning that there might be cases where it won't work. – Eilon Feb 15 '11 at 19:18
  • Great article and I'm going to use your method here on Stack Overflow. However, I think there's a bug with `_pathInfo = uri.Query;` - [`PathInfo`](http://msdn.microsoft.com/en-us/library/system.web.httprequest.pathinfo(v=vs.110).aspx) looks to be anything [between a url's extension and the `?` query string](http://www.nathanaeljones.com/blog/2008/pathinfo-woes) delimiter, e.g. `http://test.com/page.aspx/path/info?query=1` would have `PathInfo = /path/info`. Having the query in `PathInfo` was causing routes to not match for me. – Jarrod Dixon Sep 25 '14 at 22:37
  • When attempting to get the route data from a URL like `/?_=9837109823`, the `RouteInfo` constructor (at `RouteData = RouteTable.Routes.GetRouteData(InternalHttpContext);` catches the URL in built-in default route, but instead of the expected default controller, the controller is set to the query string: `_=9837109823`. Any suggestions to fix this so the query string params are properly placed in the route data? – Zarepheth Oct 28 '14 at 21:06
4

If you just want to use the MVC methods I got this to work.

            Uri returnUri = new Uri(returnUrl, UriKind.RelativeOrAbsolute);
            if (!returnUri.IsAbsoluteUri)
            {
                returnUri = new Uri("http://localhost" + returnUrl); // localhost used just to get absolute URL
            }
            HttpContextWrapper httpContext = new HttpContextWrapper(
                new HttpContext(new HttpRequest(String.Empty, returnUri.AbsoluteUri, String.Empty), new HttpResponse(TextWriter.Null))
                );
            RouteData routeData = RouteTable.Routes.GetRouteData(httpContext);
Dan
  • 41
  • 1