ASP.NET MVC: Uri to use route data

My problem is pretty simple. I have Uri, and I want to find out which route it belongs to, so I can do several checks on different sections of the route: controller, action, etc.

How to switch from Uri to RouteData or Route?

+4
source share
3 answers

Based on the direction of @tvanfosson, I came up with a class that does what I need. Note that GetRouteData actually looks at the AppRelativeCurrentExecutionFilePath and PathInfo in the RequestContextBase class, and not at the Url property.

 public class RouteInfo { public RouteInfo(RouteData data) { RouteData = data; } public RouteInfo(Uri uri, string applicationPath) { RouteData = RouteTable.Routes.GetRouteData(new InternalHttpContext(uri, applicationPath)); } public RouteData RouteData { get; private set; } //******************** //Miscellaneous properties here to deal with routing conditionals... (eg "CanRedirectFromSignIn") //******************** private class InternalHttpContext : HttpContextBase { private HttpRequestBase _request; public InternalHttpContext(Uri uri, string applicationPath) : base() { _request = new InternalRequestContext(uri, applicationPath); } public override HttpRequestBase Request { get { return _request; } } } private class InternalRequestContext : HttpRequestBase { private string _appRelativePath; private string _pathInfo; public InternalRequestContext(Uri uri, string applicationPath) : base() { _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; } } } } 
+4
source

You can try extending the HttpRequestBase and overriding the Uri property so that you can assign your Uri to the property on demand. Then override the HttpContextBase so that you can set the Request property in context. Then you can use the GetRouteData () method in the RouteCollection class to get a suitable RouteValueDictionary. Note that RouteCollection is available as a static property of the RouteTable class.

 var myRequest = new MyRequest( myUri ); var myContext = new MyContext( myRequest ); var routeData = RouteTable.Routes.GetRouteData( myContext ); 

Update

In your use case (comments) you can simply map the controller / action:

 if (myUri.ToString().ToLower().Contains( "/controller/action" )) { return RedirectToAction( action, controller, new { ...route data } ); } else { return Redirect( "http://www.example.com/default" ); } 
+2
source

Depending on how fine you are with changing the path to the current request (in my case I am), here is a very simple solution that does not include a mockery:

 HttpContext httpContext = HttpContext.Current; httpContext.RewritePath(URL); RouteData route = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext)); 
+1
source

All Articles