获得属性路线模板asp.net名单的WebAPI 2.2 [英] get a list of attribute route templates asp.net webapi 2.2
问题描述
我有一个.NET项目在C#中使用的WebAPI 2.2运行。
I have a .NET project running in C# using WebApi 2.2.
我登记所有使用属性我的路线。我想这样做的是语法上的亲检索所有的属性路线模板作为字符串。
I am registering all of my routes using attributes. What I would like to do is pro grammatically retrieve all of the attribute route templates as strings.
是这样的: VAR routeTemplates = System.Web.Routing.RouteTable.Routes.Select(X => x.RouteTemplates);
我能看到所有的路线,当我把手表上的 ControllerContext.Configuration.Routes
I am able to see all of the the routes when I put a watch on ControllerContext.Configuration.Routes
不过,我似乎无法从我的code访问的路线,因为他们是受保护的内部结构。 如何我得到他们?
However, I cannot seem to access the routes from my code as they are protected internals. How do I get at them?
我附加了屏幕截图,显示我在我的当地人观看
,我需要得到的。
I've attached a screen shot which shows the values I am seeing on my locals watch
that I need to get at.
推荐答案
在注册属性的路由中的Web API,你可以注册一个自定义的 IDirectRouteProvider
自定义如何属性的路由找到。在这种定制 IDirectRouteProvider
可以委派所有的硬的工作的默认实现, DefaultDirectRouteProvider
,看起来都控制器和动作来计算属性的路由列表,然后邀功为所有辛苦的工作。
When registering the attribute routes in Web API you can register a custom IDirectRouteProvider
to customize how attribute routes are found. In that custom IDirectRouteProvider
you can delegate all the "hard" work to the default implementation, DefaultDirectRouteProvider
, that looks at all the controllers and actions to compute the list of attribute routes, and then take credit for all that hard work.
要设置这一切了,首先创建一个新的可观察的直达供应商,将所有的工作:
To set this all up, first create a new "observable" direct route provider that delegates all its work:
public class ObservableDirectRouteProvider : IDirectRouteProvider
{
public IReadOnlyList<RouteEntry> DirectRoutes { get; private set; }
public IReadOnlyList<RouteEntry> GetDirectRoutes(HttpControllerDescriptor controllerDescriptor, IReadOnlyList<HttpActionDescriptor> actionDescriptors, IInlineConstraintResolver constraintResolver)
{
var realDirectRouteProvider = new DefaultDirectRouteProvider();
var directRoutes = realDirectRouteProvider.GetDirectRoutes(controllerDescriptor, actionDescriptors, constraintResolver);
// Store the routes in a property so that they can be retrieved later
DirectRoutes = DirectRoutes.Union(directRoutes);
return directRoutes;
}
}
然后用从 WebApiConfig.Register
法这个新类在应用程序的启动:
And then use this new class from the WebApiConfig.Register
method in your app's startup:
public static class WebApiConfig
{
public static ObservableDirectRouteProvider GlobalObservableDirectRouteProvider = new ObservableDirectRouteProvider();
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes(GlobalObservableDirectRouteProvider);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
请注意,该数据被最终存储在静态字段。这是必需的,因为里面的code WebApiConfig.Register
不立即叫 - 它后来被称为在的global.asax.cs
。所以,观察事物的结果,加上一些code到的Application_Start
:
Note that the data is ultimately stored in a static field. This is required because the code inside WebApiConfig.Register
is not called immediately - it's called later in global.asax.cs
. So, to observe the results of everything, add some code to Application_Start
:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var allDirectRoutes = WebApiConfig.GlobalObservableDirectRouteProvider.DirectRoutes;
// now do something with 'allDirectRoutes'
}
和在一个小的测试我写的,我得到了这些值:
And in a little test I wrote, I got these values:
和你有它,在应用程序中的所有属性的路由列表!
And there you have it, a list of all the attribute routes in the app!
请注意:如果你想找出其中的每个属性航线的来源有一个在每条路线的 DataTokens
财产藏匿额外的数据
Note: There's additional data squirrelled away in the DataTokens
property of each route if you want to figure out where each attribute route came from.
这篇关于获得属性路线模板asp.net名单的WebAPI 2.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!