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?
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/
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With