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?

crthompson
  • 15,653
  • 6
  • 58
  • 80
pcbliss
  • 407
  • 6
  • 15

7 Answers7

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/

Adrian Brown
  • 250
  • 3
  • 9
  • 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 Dec 01 '14 at 14:02
  • @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. – Adrian Brown Dec 29 '14 at 02:36
  • Using MVC5, had same issue with HelpPages and Unity. I made the overloaded constructor private and this solved the problem. – AaronLS Sep 15 '20 at 14:41
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

Carl
  • 1,782
  • 17
  • 24
  • Thanks, that solved my problem! Got to your answer here coming from [this thread](http://stackoverflow.com/questions/18389309/the-type-string-cannot-be-constructed), solved my problem. – Daniel Lemke May 21 '15 at 09:45
  • Thanks. This is the simplest and least impacting answer. – NER1808 Aug 27 '15 at 12:33
4

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.

David L
  • 32,885
  • 8
  • 62
  • 93
  • With my existing project this doesn't work but will try it on a fresh project asap – mare Sep 15 '13 at 22:19
  • 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. – David L Sep 15 '13 at 22:21
  • 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) – mare Sep 16 '13 at 11:47
  • Thank you for the vote and congratulations on finding the issue. – David L Sep 16 '13 at 12:45
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.

mare
  • 13,033
  • 24
  • 102
  • 191
  • 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. – SBirthare Jun 19 '15 at 08:36
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.
tomg
  • 369
  • 3
  • 6
1

Check Yao's blog regarding Help Page:

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

Kiran
  • 56,921
  • 15
  • 176
  • 161
  • 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 – mare Sep 15 '13 at 17:08
  • you can install `Microsoft.AspNet.WebApi.HelpPage` nuget package which will create an area called `HelpPage` in your MVC application. – Kiran Sep 15 '13 at 17:17
  • 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? – mare Sep 15 '13 at 17:19
  • 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? – Kiran Sep 15 '13 at 17:24
  • 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. – mare Sep 15 '13 at 17:40
  • hmm..that's strange. I know that I am stating the obvious, but you have web api routes configured in your MVC application right? and also are you able to hit this webapi controller by making actual requests? If possible could you share a repro? I can try debugging the issue. – Kiran Sep 15 '13 at 18:04
  • Yes, I do have routes configured and can hit this Web API controller. Unfortunately, I cannot share this project but I will try in another new MVC project and see if anything changes. – mare Sep 15 '13 at 18:49
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.

benmccallum
  • 1,241
  • 12
  • 27