ASP.Net的Web API - ApiExplorer不含任何ApiDescriptions [英] ASP.Net Web Api - ApiExplorer does not contain any ApiDescriptions

查看:395
本文介绍了ASP.Net的Web API - ApiExplorer不含任何ApiDescriptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我的Web服务的一个控制器的选项方法将返回包含与控制器相关的URI的端点有效的HTTP方法的消息。我的选项方法看起来是这样的:

I am trying to implement an Options method in a controller of my web service that will return a message containing the valid HTTP methods for the URI endpoint associated with the controller. My Options method looks something like this:

public HttpResponseMessage Options()
{
    var resp = new HttpResponseMessage();
    resp.Content = new StringContent("");

    var apiExplorer = GlobalConfiguration.Configuration.Services
        .GetApiExplorer();

    foreach (ApiDescription api in apiExplorer.ApiDescriptions)
    {
        resp.Content.Headers.Add("Allow", api.HttpMethod.Method);
    }

    return resp;
}

我曾尝试在一个全新的Web API项目上面的方法:用GET,POST和DELETE方法控制器内(蕴涵不变的路由)。正如预期的那样,与允许:GET,POST和DELETE返回响应。我有麻烦,但是,添加这个到我工作的一个更大的项目。在较大的项目中,内ApiExplorer的ApiDescriptions列表中不包含任何元素。为什么是这样?我怀疑这是由于已实施的自定义路由,虽然这种怀疑的唯一依据是以下链接:

I have tried the above method in a brand-new Web Api project (implication: unaltered routing) inside of a controller with Get, Post, and Delete methods. As expected, a response with "Allow: GET, POST, DELETE" is returned. I am having trouble, however, adding this to a larger project that I am working on. In the larger project, the ApiDescriptions list within ApiExplorer does not contain any elements. Why is this? I suspect it is due to the custom routing that has been implemented, although the only basis for that suspicion is the following link:

http://forums.asp.net/t/1821651.aspx/1

有其他人经历过这个空ApiDescription名单?如果是这样,你有没有发现一个补救措施?

Has anybody else experienced this empty ApiDescription list? If so, did you find a remedy?

请注意:我使用的MCV 4 RC

Note: I am using MCV 4 RC

推荐答案

如果您使用的一瞥,你可能需要禁用它的航线检查:

If you use Glimpse, you might have to disable it's route inspector:

<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
  <logging level="Off" />
  <tabs>
    <ignoredTypes>
      <add type="Glimpse.AspNet.Tab.Routes, Glimpse.AspNet" />
    </ignoredTypes>
  </tabs>
  <inspectors>
    <ignoredTypes>
      <add type="Glimpse.AspNet.Inspector.RoutesInspector, Glimpse.AspNet" />
    </ignoredTypes>
  </inspectors>
</glimpse>

管窥创建于HostedHttpRouteCollection打破枚举RouteProxies:
<一href=\"https://github.com/mono/aspnetwebstack/blob/master/src/System.Web.Http.WebHost/Routing/HostedHttpRouteCollection.cs\" rel=\"nofollow\">https://github.com/mono/aspnetwebstack/blob/master/src/System.Web.Http.WebHost/Routing/HostedHttpRouteCollection.cs

我知道链接是单声道,但同样适用于标准的.NET。

I know the link is for mono but the same is true for standard .Net.

这篇关于ASP.Net的Web API - ApiExplorer不含任何ApiDescriptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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