加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-web-api – Web API帮助页面和API Explorer返回0描述

发布时间:2020-12-16 09:41:29 所属栏目:asp.Net 来源:网络整理
导读:我有这个项目只是一个Web API项目.在过去的某个时候,我删除了HelpPages,我让应用程序使用了OWIN. 现在我被要求添加API HelpPages,我已经完成了. 我已将我的Startup类设置为看起来像这样: public void Configuration(IAppBuilder app){ // Needs to be first
我有这个项目只是一个Web API项目.在过去的某个时候,我删除了HelpPages,我让应用程序使用了OWIN.
现在我被要求添加API HelpPages,我已经完成了.
我已将我的Startup类设置为看起来像这样:

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)一切正常.我希望这对你也有帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读