11

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?

7 Answers 7

9

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/

Sign up to request clarification or add additional context in comments.

3 Comments

I would say a better solution is the one that I've answered with. Rather than remove it make is protected. Not sure which version of MVC you're using, but in 5.2.2 the parameterless constructor calls the one that you are suggesting is removed.
@Carl You might be right, but when I answered this (over a year ago), MVC 5 was only RC. I would have been referring to v4, as the OP stated in his question.
Using MVC5, had same issue with HelpPages and Unity. I made the overloaded constructor private and this solved the problem.
7

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

2 Comments

Thanks, that solved my problem! Got to your answer here coming from this thread, solved my problem.
Thanks. This is the simplest and least impacting answer.
4
+200

My assumption is that the requirement is to provide automated documentation for Web API endpoints, not MVC endpoints. Based on that assumption, I built a POC that works as intended and I can always email you a zip if needed :).

  1. Build a new MVC 4 Internet Application. This will generate Home and Account controllers, matching views, and MVC routing.

  2. Add in Microsoft.AspNet.WebApi.HelpPage package by searching for "HelpPage" in the Online section of the Package Manager.

  3. Add in a new folder for your Web API controllers called "Api". This is driven by your WebApi routing set up in the Register method of WebApiConfig.cs. Mine is as follows:

    public static void Register(HttpConfiguration 
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional });
    }
    
  4. Build the project. Hit endpoint /help. It spit out all of the end points in my api controller (a get and a post endpoint).

This did not require any changes to the area folder that is generated by adding the HelpPages package. All it required was adding a new Api folder and a new Api controller (empty) to the folder, and a quick two endpoints built out into the new controller.

4 Comments

With my existing project this doesn't work but will try it on a fresh project asap
If it isn't working then I suspect you have some dependency missing. Create a fresh project and check for missing dlls? That or perhaps you have unique routing or global.asax configuration.
I am upvoting your answer because it is the most detailed one actually saying it should work and it does (in fresh MVC projects, unless you have installed something that would block ApiExplorer, which happened in my case)
Thank you for the vote and congratulations on finding the issue.
3

I can confirm the answers before - the HelpPage does pick up any controller no matter where it is located if it matches routing and inherits from ApiController.

I spent hours hunting this one down. I figured, it must be something I added to the project that's messing up my routes/APIs. So I found it after hours of installing/deinstalling various Nuget packages. The problem with my project was that I had Glimpse.Mvc4 package installed. This package somehow prevented the ApiExplorer to pick up my APIs. This is obviously a bug within the Glimpse.Mvc4 (or Glimpse.AspNet) packages or a bug within ApiExporer (I didn't go into trouble finding out what the bug was).

I'll report this to the Glimpse team. Posting this answer here maybe will help someone else too.

1 Comment

As on today, Glimpse.Mvc4 was causing issue and I had to remove it. I can actually try and install latest version (that may solve problem). Thanks for your analysis, it helped.
2
  1. In the NuGet package manager, search for "helppage" among the Online packages.
  2. Install it (it keeps getting updated and it does all you need even if you have the latest .net)
  3. In will add an Area and in that create all the necessary files you need. Build it and test it under [your_root]/Help
  4. Watch the video on Yao's blog here to get a quick overview on how you can customize it. The video is a bit old, it is still valid.

Comments

1

Check Yao's blog regarding Help Page:

http://blogs.msdn.com/b/yaohuang1/

7 Comments

all the samples work within Web API standalone project, there isn't a sample where he'd use the HelpPage from within the existing MVC project
you can install Microsoft.AspNet.WebApi.HelpPage nuget package which will create an area called HelpPage in your MVC application.
you can install it, yes, and I did but it is not picking up any Api controllers for documentation so docs are empty, maybe you can prove otherwise?
can you share information about your routes and also the controller (you need not share the entire action implementation code itself, just the action defintion should be enough)...btw, what is the version of Microsoft.AspNet.WebApi nuget package that you have?
docs do get created for the same controller code when that controller is in a standalone Web API project so I guess nothing's wrong with the code. I also had it in root/Controllers and in Areas/Area/Controllers. It doesn't get picked up by the HelpPage code. In my MVC project the WebAPI nuget package is 4.0.30506 which is the latest one.
|
0

I just ran into this issue. Here's what it was for me.

I'd had the Help pages working on a previous project (w/ MVC 5 and Web API 2) so I knew it was something different in my new project. Turned out in the old (working) project I was only doing DI in the WebApi controllers, not the MVC controllers. I didn't need DI in the MVC controllers, so I simply commented out the code bootstrapping the UnityDependencyResolver as the MVC dependency resolver (in my case the UnityMvcActivator class).

Obviously this won't be an option for everyone, but where not, @Adrian Brown's answer looks like your best bet until this is fixed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.