Web API help pages and API APIs return 0 descriptions

I have this project, which is just a web API project. At some point in the past, I uninstalled HelpPages, and I made an OWIN application. Now I was asked to add the API help pages in which I did this. I set up the Startup class to look something like this:

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);
}

So my route for help pages works. As far as I can tell, this should work, but the problem is that ApiExplorer does not drop any descriptions.

In my ConfigureWebApi method, I delete the formatting, I commented on this, but still does not work, here is this method:

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.

, , . , - , . , , !

+4
2

.

GlobalConfiguration.Configure(WebApiConfig.Register)

Startup ( global.asax) . , .

+1

WebApiConfig.Register, Owin WebApi, , , .

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

Source: https://habr.com/ru/post/1598311/


All Articles