title: How to find the route that matches a url?
body: <p>How can I find the route that matches a string url in ASP.NET MVC?</p>
  
  <p>This is not for the purposes of debugging.</p>
  
  <p>I am generating a link using <code>Request.UrlReferrer</code> but we need to restrict this to specific controller actions in our application. Ideally we would so something like so:</p>
  
  <pre><code>@Url.PreviousUrl(
      default: Url.Action("index", "home")
      validRoutes: new[] { 
          new { controller = "list", "projects" }
          new { controller = "tagged", "projects" },
          new { controller = "details", "category" }
      }
  );
  </code></pre>
  
  <p>If the <code>UrlReferrer</code> matches any of those routes then we will redirect to it, otherwise we will redirect to the default url specified.</p>