ASP.Net Web API Help Page Area returning empty output
Asked Answered
M

4

9

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.

Mephitis answered 16/10, 2013 at 20:59 Comment(0)
A
7

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.

Ankylose answered 17/10, 2013 at 13:34 Comment(1)
Thanks for te reply but I don't think I'm using glimpse!Mephitis
E
5

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.

Energid answered 25/5, 2014 at 17:45 Comment(4)
where does InjectionConstructor constructor come from? Unity?Slugabed
@ChrisMcKelt yes Chris from UnityEnergid
Solution 1 didn't work for me, because it was causing a null reference exception. Solution 2 works fine, as long as you add a reference to Microsoft.Practices.Unity. Obviously depending on your project this may or may not be acceptable, but in my case it is so thank for the help!Jodhpurs
For Solution 2: instead of the injection attribute, use a factory when configuring Unity container.RegisterFactory<HttpConfiguration>(c => GlobalConfiguration.Configuration);, this removes the dependency on UnityDeery
I
2

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

Ir answered 9/11, 2015 at 19:5 Comment(0)
N
0

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

Naashom answered 15/8, 2017 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.