Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Web API and API documentation to an existing MVC project

I have successfully added a web api controller to an existing MVC4 application.

I would like to have the api documentation functionality as is available in the new web api samples (ex. http://sample.hostname.com/help). I believe these use the ApiExplorer class. I tried just copying the HelpPage area into my project, but I get an error

"The type String cannot be constructed. You must configure the container to supply this value"

when I try to navigate to help.

What must I do to add automated documentation of the API?

like image 284
pcbliss Avatar asked Nov 26 '12 20:11

pcbliss


2 Answers

As others have already said, you must first install the NuGet package Microsoft.AspNet.WebApi.HelpPage. This will create a new HelpPage Area in your application with the views, models and controllers required to display the documentation.

However, the HelpController that is generated contains an overloaded constructor:

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

This doesn't seem to play well with Unity (or probably any kind of DI container). You get this error:

[InvalidOperationException: The type String cannot be constructed. You must configure the container to supply this value.]

Remove this overload and it should work.

Also just found another reference to this: http://www.stefan-scheller.com/2013/08/the-type-string-cannot-be-constructed-web-api-with-unity/

like image 89
Adrian Brown Avatar answered Oct 26 '22 20:10

Adrian Brown


V5.2.2 has the following code:

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

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

in the HelpController

Unity uses the constructor with the most arguments, so to get it to use the parameterless constructor I changed the second overload to protected:

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

And the help pages are back

like image 23
Carl Avatar answered Oct 26 '22 19:10

Carl