Adding Web API and API documentation to an existing MVC project

后端 未结 7 926
时光说笑
时光说笑 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: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.

提交回复
热议问题