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.
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
.
精彩评论