r3plica
r3plica

Reputation: 13397

Web API Help Pages and API Explorer returning 0 descriptions

I have this project that is just a Web API project. At some point in the past I removed the HelpPages and I made the app use OWIN. Now I have been asked to add API HelpPages back in which I have done. I have set my Startup class to look a bit 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 that my route for the help pages is working. As far as I can tell, that should just work but the problem is that the ApiExplorer doesn't pull back any descriptions.

In my ConfigureWebApi method I remove formatting, I have commented that out but and still it doesn't work, here is the 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 }
    );
}

I actually edited the HelpController and put a breakpoint on the return view line which is how I know the ApiExplorer has no descriptions:

public ActionResult Index()
{
    var docProdivder = Configuration.Services.GetDocumentationProvider();
    var desciptions = Configuration.Services.GetApiExplorer().ApiDescriptions;

    ViewBag.DocumentationProvider = docProdivder;
    return View(desciptions);
}

I read somewhere that if I do 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();

    var exploerer = new ApiExplorer(httpConfig);
    var descriptions = exploerer.ApiDescriptions;

    // Register all areas
    AreaRegistration.RegisterAllAreas();

    ConfigureOAuthTokenGeneration(app);
    ConfigureOAuthTokenConsumption(app);
    ConfigureWebApi(httpConfig);
    app.UseWebApi(httpConfig);
}

I should see the descriptions, but it still doesn't work. So then I read somewhere else to set my project to output the xml description file and configure the HelpPageConfig file to use the documentProvider. I generated the Xml description file and can verify that my descriptions are in there, here is a snippet:

    <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>

I uncommented out the lines in the HelpPageConfig like this:

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

and made sure that the XML file was in the App_Data folder. The names are all correct, but when I run my project I still get no descriptions from the ApiExplorer.

So, as you can see I am at my wits end. I hope that someone has come across this before and knows how to fix it. If you do, please help!

Upvotes: 4

Views: 1111

Answers (2)

user2981810
user2981810

Reputation: 389

If you don't have access to WebApiConfig.Register, which I didn't with my Owin WebApi project, the following code seems to work for me.

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

Upvotes: 0

user5546600
user5546600

Reputation: 11

I have the same problem. If I added

GlobalConfiguration.Configure(WebApiConfig.Register)

in Startup class (I don't use global.asax) everything worked properly. I hope this will help you, too.

Upvotes: 1

Related Questions