如何检查给定路径的路由(ASP.NET MVC)是否存在? [英] How can I check if a route (ASP.NET MVC) exists for a given path?

查看:169
本文介绍了如何检查给定路径的路由(ASP.NET MVC)是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地URL列表,我需要确定它们是否为有效的MVC路径".如何检查URL(路径)是否映射到MVC控制器?

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的Route Debugger 将找到一条路由匹配当前请求,并使用当前HttpContext进行匹配.我想在不建立模拟HttpContext的情况下获取此信息-如果可能的话.

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.

推荐答案

您可以使用模拟的HttpContextBase调用RouteTable.Routes.GetRouteData.

You can call RouteTable.Routes.GetRouteData with a mocked HttpContextBase.

使用请求的AppRelativeCurrentExecutionFilePath在内部匹配路由.
但是,此功能未公开,因此您需要传递HttpContextBase.

The routes are matched internally using the request's AppRelativeCurrentExecutionFilePath.
However, this functionality is not exposed, so you need to pass an HttpContextBase.

您需要创建一个HttpContextBase类,该类在其request属性中返回一个HttpRequestBase实例.
HttpRequestBase类需要在其AppRelativeCurrentExecutionFilePath属性中以~/开头返回路径.

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.

您不需要实现任何其他属性,除非它们被IRouteConstraint所使用.

You don't need to implement any other properties, unless they're used by IRouteConstraints.

要检查您是否有MVC路由,请检查生成的routeData.Handler is MvcRouteHandler.

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

这篇关于如何检查给定路径的路由(ASP.NET MVC)是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆