Web API帮助页和API资源管理器返回0说明



我有一个项目,它只是一个Web API项目。在过去的某个时候,我删除了帮助页面,并让应用程序使用OWIN。现在我被要求添加API帮助页面,我已经做了。我已经将启动类设置为如下所示:

public void Configuration(IAppBuilder app)
{
    // Needs to be first
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
    var httpConfig = new HttpConfiguration();
    // Register all areas
    AreaRegistration.RegisterAllAreas();
    ConfigureOAuthTokenGeneration(app);
    ConfigureOAuthTokenConsumption(app);
    ConfigureWebApi(httpConfig);
    app.UseWebApi(httpConfig);
}

这样我的帮助页面就可以正常工作了。据我所知,这应该是有效的,但问题是ApiExplorer没有撤回任何描述。

在我的ConfigureWebApi方法中,我删除了格式化,我已经注释掉了,但仍然不起作用,下面是方法:

private void ConfigureWebApi(HttpConfiguration config)
{
    // Web API configuration and services
    var formatters = config.Formatters;
    var jsonFormatter = formatters.JsonFormatter;
    var serializerSettings = jsonFormatter.SerializerSettings;
    // Remove XML formatting
    formatters.Remove(config.Formatters.XmlFormatter);
    jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
    jsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
    // Configure our JSON output
    serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    serializerSettings.Formatting = Formatting.Indented;
    serializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    serializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
    // Web API routes
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

实际上,我编辑了HelpController,并在返回视图行上放置了一个断点,这就是我知道ApiExplorer没有描述的原因:

public ActionResult Index()
{
    var docProdivder = Configuration.Services.GetDocumentationProvider();
    var desciptions = Configuration.Services.GetApiExplorer().ApiDescriptions;
    ViewBag.DocumentationProvider = docProdivder;
    return View(desciptions);
}

我在某个地方读到,如果我这样做:

public void Configuration(IAppBuilder app)
{
    // Needs to be first
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
    var httpConfig = new HttpConfiguration();
    var exploerer = new ApiExplorer(httpConfig);
    var descriptions = exploerer.ApiDescriptions;
    // Register all areas
    AreaRegistration.RegisterAllAreas();
    ConfigureOAuthTokenGeneration(app);
    ConfigureOAuthTokenConsumption(app);
    ConfigureWebApi(httpConfig);
    app.UseWebApi(httpConfig);
}

我应该看看描述,但它仍然不起作用。因此,我阅读了其他地方的内容,将我的项目设置为输出xml描述文件,并将HelpPageConfig文件配置为使用documentProvider。我生成了Xml描述文件,可以验证我的描述是否在其中,这里有一个片段:

    <member name="T:Melanite.Controllers.CollectionsController">
        <summary>
        Controller for all collection related functions
        </summary>
    </member>
    <member name="M:Melanite.Controllers.CollectionsController.#ctor">
        <summary>
        Default constructor
        </summary>
    </member>
    <member name="M:Melanite.Controllers.CollectionsController.Get(System.Int32)">
        <summary>
        Get all the collections for the given center
        </summary>
        <param name="centerId">The id of the center that the collection belongs to</param>
        <returns>A list of collections</returns>
    </member>
    <member name="M:Melanite.Controllers.CollectionsController.Get(System.Int32,System.DateTime)">
        <summary>
        Get all the collections for the given center on a specific date
        </summary>
        <param name="centerId">The id of the center that the collection belongs to</param>
        <param name="date">The planned collection date for the collections</param>
        <returns>A list of collections</returns>
    </member>

我取消了HelpPageConfig中的行注释,如下所示:

// Uncomment the following to use the documentation from XML documentation file.
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

并确保XML文件在App_Data文件夹中。这些名称都是正确的,但当我运行我的项目时,我仍然没有从ApiExplorer得到任何描述。

所以,正如你所看到的,我已经无计可施了。我希望有人以前遇到过这个问题,知道如何解决。如果你遇到了,请帮忙!

我也有同样的问题。如果我添加

GlobalConfiguration.Configure(WebApiConfig.Register(

在Startup类(我不使用global.asax(中,一切都正常工作。我希望这对你也有帮助。

如果你没有访问WebApiConfig.Register的权限,而我在Owin WebApi项目中没有访问权限,那么下面的代码似乎对我有效。

GlobalConfiguration.Configure((config) => { config.MapHttpAttributeRoutes(); });

最新更新