简体   繁体   English

如何在asp.net MVC中获取所有已配置的WebApi路由的列表?

[英]How do I get a list of all configured WebApi routes in asp.net MVC?

I'm trying to pass configuration values to bootstrap a single page AngularJs app hosted within an MVC app that utilises WebApi (see below from the Razor view, this is achieved by inheriting a BasePage and dependency injecting a Serializer/FinancialYears service). 我正在尝试传递配置值以引导在利用WebApi的MVC应用程序中托管的单个页面AngularJs应用程序(请参阅下面的Razor视图,这是通过继承BasePage和依赖项注入Serializer / FinancialYears服务来实现的)。

<script>
var angularInitConfig = @Html.Raw(Serializier.Serialize(new {
                   financialYears = FinancialYearsService.GetFinancialYears()
               }));
</script>

This works perfectly, however I would really like to be able to extend it to include the routes from my WebApi app to avoid having to configure the endpoints in both the WebApi app AND the AngularJs app individually. 这完美地工作了,但是我真的很希望能够对其进行扩展,使其包含来自我的WebApi应用程序的路由,从而避免必须分别在WebApi应用程序和AngularJs应用程序中配置端点。

Having poked around in the RouteTable.Routes class I can see that the information I require is available and accessible from within the view, however I've been unable to extract it. RouteTable.Routes类中查找后,我可以看到所需的信息在视图中可用并且可以访问,但是我无法提取它。

So what I'd ideally like to do is generate a collection of objects as defined below from the RouteTable.Routes class, serialize them and spit them out in the bootstrap config for the AngularJS app to consume. 因此,我理想地要做的是从RouteTable.Routes类生成如下定义的对象的集合,对其进行序列化并将其吐入bootstrap配置中,以供AngularJS应用使用。

new {
    name = "SomeService",
    route = "api/{financialYearId}/someservice",
    method = "POST"
}

Does anybody have an idea how to extract this information from RoutesTable.Routes ? 有人知道如何从RoutesTable.Routes提取此信息吗? Is there an easier way to generate the data required? 有没有更简单的方法来生成所需数据?

NB. 注意 All WebApi routes are configured explicitly using the Routes attribute as such: 所有WebApi路由都是使用Routes属性明确配置的,如下所示:

[HttpGet]
[Route("api/{financialYearId}/someservice")]

如果使用Visual Studio创建默认模板asp.net Mvc或WebAPi,则将在文件夹> Areas \\ HelpPage ...中获得帮助,并且如果您访问的应用程序位于: Http://yourportapplication/api/Help如果为webapi)。然后,您可以看到代码如何获取信息...只是为了开始您想要的...。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM