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
  • 27,092
  • 26
  • 73
  • 78

1 Answers1

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