How can I find the route that matches a string url in ASP.NET MVC?
This is not for the purposes of debugging.
I am generating a link using Request.UrlReferrer but we need to restrict this to specific controller actions in our application. Ideally we would so something like so:
@Url.PreviousUrl(
default: Url.Action("index", "home")
validRoutes: new[] {
new { controller = "list", "projects" }
new { controller = "tagged", "projects" },
new { controller = "details", "category" }
}
);
If the UrlReferrer matches any of those routes then we will redirect to it, otherwise we will redirect to the default url specified.
You can call GetRouteData as follows. There may be a better way, but I’ve used similar in unit testing
http://bradwilson.typepad.com/blog/2010/07/testing-routing-and-url-generation-in-aspnet-mvc.html