.net core
.net core overview

Implementing Swagger in ASP.NET MVC 5: A Complete Guide

Swagger in ASP.NET MVC Web API

It is basically used for Test the API and documentation it allows you to describes the structure of your APIS so that system can read. Swagger is mainly come with asp.net core but we can also integrate swagger in asp MVC web API in this blog we can understand how  we integrated swagger in asp MVC web api easily we have to follow some step

Implementing Swagger in an ASP.NET MVC 5 Web API project can significantly enhance your API documentation and testing capabilities. Here’s a step-by-step guide to help you set it up:

Step 1: Create a New ASP.NET MVC 5 Web API Project Visual Studio 2019

  1. Open Visual Studio 2019 .
  2. Create a New Project:
    1. Select ASP.NET Web Application .
    1. Choose MVC and check Web API.
ASP MVC

In this step, we need to create a project, so choose ASP.NET Web Application (.NET Framework) option.

asp mvc part 2
ASP MVC PART 3

Step 2: Install Swagger

  1. Open the Package Manager Console in Visual Studio:
    • Go to Tools > NuGet Package Manager > Package Manager Console.
  2. Install Swagger: Run the following command in the console:

we need to install the package named Swashbuckle with following command: Install-Package Swashbuckle

Swashbuckle

Step 3: Configure Swagger

  1. Open the SwaggerConfig.cs file:
    • After installing the package, a SwaggerConfig.cs file is created in the App_Start folder.
  2. Modify SwaggerConfig.cs: You can customize the settings. Here’s a basic configuration:

once we install Swashbuckle package , a swaggerconfig.cs file is created in App_Start folder so you need to modified some lines of code .

using System.Web.Http;
using WebApi.Swagger;

public class SwaggerConfig
{
    public static void Register()
    {
        var thisAssembly = typeof(SwaggerConfig).Assembly;

        GlobalConfiguration.Configuration
            .EnableSwagger(c =>
            {
                c.SingleApiVersion("v1", "Your API Title");
                c.IncludeXmlComments(GetXmlCommentsPath());
            })
            .EnableSwaggerUi();
    }

    private static string GetXmlCommentsPath()
    {
        return System.String.Format(@"{0}\bin\YourApiName.xml", System.AppDomain.CurrentDomain.BaseDirectory);
    }
}

Step 4: Enable XML Comments (Optional)

  1. Enable XML Documentation:
    • Right-click on your project in Solution Explorer and select Properties.
    • Go to the Build tab and check XML documentation file.
    • Name it (e.g., YourApiName.xml).
xml documentation

Step 5: Initialize Swagger in Application Start

Open Global.asax.cs: Add the Swagger registration in the Application_Start method in this step open this file configure global. Sax file see below code .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace SwaggerInMVC
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
           
        }
    }
}

Step 6: Run and Test Your Application

  1. Start your project by pressing F5 or Ctrl+F5.
  2. Access Swagger UI: Navigate to http://localhost:<port>/swagger/ui/index (replace <port> with your application port).

Conclusion

Follow these steps, you have a functional Swagger implementation in your ASP.NET MVC 5 Web API project. This will help you document and test your API effectively

Reference:

Swagger Documentation

for more details please visit apitpoint

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *