Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I solved this by adding a custom RouteInfo class which creates a new HttpContext with the supplied url and application path and uses that to obtain an instance of RouteData based on the new HttpContext object. I can then evaluate the Controller and Action values to see which route was matched. I have this wired up to an extension method on the Uri class. It feels a bit hackish and I was hoping there was a cleaner way to do this so I'll leave the question open in case someone else has a better solution.</p> <p><strong>ROUTEINFO CLASS:</strong></p> <pre><code>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; } private class InternalHttpContext : HttpContextBase { private readonly 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 readonly string _appRelativePath; private readonly string _pathInfo; public InternalRequestContext(Uri uri, string applicationPath) : base() { _pathInfo = ""; //uri.Query; (this was causing problems, see comments - Stuart) if (String.IsNullOrEmpty(applicationPath) || !uri.AbsolutePath.StartsWith(applicationPath, StringComparison.OrdinalIgnoreCase)) _appRelativePath = uri.AbsolutePath; else _appRelativePath = uri.AbsolutePath.Substring(applicationPath.Length); } public override string AppRelativeCurrentExecutionFilePath { get { return String.Concat("~", _appRelativePath); } } public override string PathInfo { get { return _pathInfo; } } } } </code></pre> <p><strong>URI EXTENSION METHOD:</strong></p> <pre><code> /// &lt;summary&gt; /// Extension methods for the Uri class /// &lt;/summary&gt; public static class UriExtensions { /// &lt;summary&gt; /// Indicates whether the supplied url matches the specified controller and action values based on the MVC routing table defined in global.asax. /// &lt;/summary&gt; /// &lt;param name="uri"&gt;A Uri object containing the url to evaluate&lt;/param&gt; /// &lt;param name="controllerName"&gt;The name of the controller class to match&lt;/param&gt; /// &lt;param name="actionName"&gt;The name of the action method to match&lt;/param&gt; /// &lt;returns&gt;True if the supplied url is mapped to the supplied controller class and action method, false otherwise.&lt;/returns&gt; 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 &amp;&amp; routeInfo.RouteData.Values["action"].ToString() == actionName); } } </code></pre> <p><strong>USAGE:</strong></p> <pre><code>Uri url = new Uri("http://www.website.com/find-your-new-rental/northerncalifornia/sacramento.html"); if (url.IsRouteMatch("FindYourNewRental", "Community")) { // do something } </code></pre> <p><strong>OR</strong></p> <pre><code>if (Request.Url.IsRouteMatch("FindYourNewRental", "Community")) { // do something } </code></pre> <p><strong>ADDED BONUS:</strong> Because the RouteInfo class gives me back an instance of RouteData, I can access the route parameters as well. This led to the creation of another Uri extension method:</p> <pre><code>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; } </code></pre> <p>Which can now be used like so:</p> <pre><code>string someValue = url.GetRouteParameterValue("ParameterName"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload