ASP.Net Web API Help Page Area returning empty output

℡╲_俬逩灬. 提交于 2019-12-09 09:28:19

问题


I have a preexisting MVC app that I added Web API and Web API Self Documentation using Nuget. While the Web API controllers function fine (return valid responses to HTTP requests) the Help controller is not finding any Web API methods to document.

In the Help controller Index action "Configuration.Services.GetApiExplorer().ApiDescriptions" is returning with 0 results.

What populated ApiDescriptions and are there any config settings I need to set to expose my api to documentations?

The Help Area is a separate area from the rest of my application. Is this causing the piece that finds the Controllers to not find my controllers? Furthermore, I even added a help snipped to the HelpController itself, which still resulted in no API descriptions.

I do also have special routing for my API controllers, so I'm not sure if that's relevant.


回答1:


After some more searching i found this post which also refers to this post

As mentioned in the first post, Glimpse is the culplit, this workaround solved the issue for me:

<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
<inspectors>
   <ignoredTypes>
      <add type="Glimpse.AspNet.Inspector.RoutesInspector, Glimpse.AspNet"/>
   </ignoredTypes>
</inspectors>
</glimpse>

This is also a known issue and the workaround is described on this Glimpse GitHub Issue.




回答2:


I have the same problem and i don't use Glimpse and i solve the problem like this:

In the ProjectName\Areas\HelpPage\Controllers\HelpController.cs file comment the constructors because is not called the implicit constructor public HelpController() : this(GlobalConfiguration.Configuration) default is called the constructor with the parameter public HelpController(HttpConfiguration config) and this initialization of the Configuration property is incorect. And you cand solve this problem like this:

Solution 1: Comment/Remove the constructors.

public class HelpController : Controller
        {
            private const string ErrorViewName = "Error";

    //        public HelpController()
    //            : this(GlobalConfiguration.Configuration)
    //        {
    //        }

    //        public HelpController(HttpConfiguration config)
    //        {
    //            Configuration = config;
    //        }

            /// <summary>
            /// GlobalConfiguration By default
            /// </summary>
            protected static HttpConfiguration Configuration
            {
                get { return GlobalConfiguration.Configuration; }
            }

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

Solution 2: inject the default constructor by add this attribute [InjectionConstructor].

public class HelpController : Controller
    {
        private const string ErrorViewName = "Error";

        [InjectionConstructor]
        public HelpController()
            : this(GlobalConfiguration.Configuration)
        {
        }

        public HelpController(HttpConfiguration config)
        {
            Configuration = config;
        }

        /// <summary>
        /// GlobalConfiguration By default
        /// </summary>
        protected static HttpConfiguration Configuration { get; private set; }
....

And problem solved.




回答3:


I was able to solve this by adding GlobalConfiguration.Configure (WebApiConfig.Register); in my Application_Start () method. Because my application uses OWIN I was registering my APIs only in Startup.Configuration (IAppBuilder app).




回答4:


After installing HelpPages package from NuGet package manager- Navigate to WebApplication1\Areas\HelpPage\App_Start\HelpPageConfig.cs and uncomment the line below

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

Also add App_Data/XmlDocument.xml to WebApplication > Properties > Build > Check XML Documentation File



来源:https://stackoverflow.com/questions/19413786/asp-net-web-api-help-page-area-returning-empty-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!