简体   繁体   中英

WebApi Help Page not reading from XML file

I have added the .net webapi help pages nuget project to our webapi to generate the help docs.

I added as follows on the command line;

Install-Package Microsoft.AspNet.WebApi.HelpPage

Its the current version installed. I then set the output of the webapi project to output to

App_Data/XmlDocument.xml

This is following the example here;

WebApi Help Tutorial

and this is being generated fine.

I then un-commented the line;

config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

again as per the link.

However, when I run my api and navigate to the webapi/help url. The model being returned is null? (no errors appear to be being thrown either).

I then have the header of the layout being displayed, but no api documentation?

One thing to add is I am using OAuth in this api. (Although its resolving the route so can this be causing any issues?) For reference this is my startup.cs

    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        LoggingConfig.RegisterLogger();

        config.DependencyResolver = new UnityDependencyResolver(
            UnityConfig.GetConfiguredContainer());


        ConfigureOAuth(app);

        WebApiConfig.Register(config);
        app.UseCors(CorsOptions.AllowAll);
        app.UseWebApi(config);

        AreaRegistration.RegisterAllAreas();

    }

Ok,

I managed to sort it, and it was due to the fact Startup.cs is being used instead.

Hence I need to change it to use Startup.HttpConfiguration as opposed to GlobalConfiguration.Configuration

Heres my updated Startup.cs

public static HttpConfiguration HttpConfiguration { get; private set; }
    public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; private set; }

    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration = new HttpConfiguration();

        LoggingConfig.RegisterLogger();

        HttpConfiguration.DependencyResolver = new UnityDependencyResolver(
            UnityConfig.GetConfiguredContainer());


        ConfigureOAuth(app);

        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(HttpConfiguration);
        app.UseCors(CorsOptions.AllowAll);
        app.UseWebApi(HttpConfiguration);
    }

I then had to modify the following help files;

HelpPageAreaRegistration.RegisterArea

specifically, to be;

        public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "HelpPage_Default",
            "Help/{action}/{apiId}",
            new { controller = "Help", action = "Index", apiId = UrlParameter.Optional });

        HelpPageConfig.Register(Startup.HttpConfiguration);
    }

and the

HelpController.cs

    public HelpController()
        : this(Startup.HttpConfiguration)
    {
    }

Hopefully this helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM