I have a list of local URL’s and I need to determine if they are “valid MVC paths”. How can I check if a URL (path) maps to a MVC controller?
Phil Haack's Route Debugger will find a route that match the current request and does so using the current HttpContext. I would like to get this info without building up a mock HttpContext - if possible.
Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.
ASP.NET MVC offers two approaches to routing: The route table, which is a collection of routes that can be used to match incoming requests to controller actions.
You can check the Request. HttpMethod property.
You can call RouteTable.Routes.GetRouteData
with a mocked HttpContextBase.
The routes are matched internally using the request's AppRelativeCurrentExecutionFilePath
.
However, this functionality is not exposed, so you need to pass an HttpContextBase
.
You need to create an HttpContextBase
class which returns an HttpRequestBase
instance in its request property.
The HttpRequestBase
class needs to return your path, beginning with ~/
, in its AppRelativeCurrentExecutionFilePath
property.
You don't need to implement any other properties, unless they're used by IRouteConstraint
s.
To check whether you got an MVC route, check whether the resulting routeData.Handler is MvcRouteHandler
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With