开发者

How can I check if a route (ASP.NET MVC) exists for a given path? [duplicate]

This question already has answers here: 开发者_Python百科 How to get RouteData by URL? (2 answers) Closed 8 years ago.

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 IRouteConstraints.

To check whether you got an MVC route, check whether the resulting routeData.Handler is MvcRouteHandler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜