Adding Web API and API documentation to an existing MVC project

后端 未结 7 927
时光说笑
时光说笑 2021-02-14 03:01

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

I would like to have the api documentation functionality as is

相关标签:
7条回答
  • 2021-02-14 03:41

    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.

    0 讨论(0)
  • 2021-02-14 03:44

    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/

    0 讨论(0)
  • 2021-02-14 03:49

    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

    0 讨论(0)
  • 2021-02-14 03:52

    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.

    0 讨论(0)
  • 2021-02-14 03:53
    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.
    0 讨论(0)
  • 2021-02-14 03:54

    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.

    0 讨论(0)
提交回复
热议问题