Skip to main content

ASP.NET Boilerplate: an alternative guide to integrate Swagger

Swagger is an excellent tool for API exploration of our RESTful APIs. In this post, we'll cover how to integrate and setup Swagger in an  ASP.NET Boilerplate (ABP) application (with ASP.NET MVC 5.x framework), to provide client-side documentation of its ABP application services. Most of all, this is an alternative documentation to the proposed in its official web site.

Once Swagger is ready, you can get documentation about your API at http://localhost:6634/swagger.


This guide will take a the project WebApiServerDemo as sample and it can be downloades at https://github.com/gezanoletti/WebApiServerDemo.git.

Step 1. Install dependencies

Install Swashbuckle.Core on your Web project. We recommend version 5.5.3, the current version at now.



Install Swashbuckle.Core.Extension on your Web project. Remark: I recommend you install version 1.0.0 because I have found errors with superior verions.

Step 2. Set up Swagger

Set up Swagger in your Web project module. The following is the basic configuration.

public class WebApiServerDemoWebModule : AbpModule
    {
        public override void PreInitialize()
        {
            //...
            ConfigureSwaggerUi();
        }        

        public override void Initialize() {...}

        private void ConfigureSwaggerUi()
        {
            Configuration.Modules.AbpWebApi().HttpConfiguration
                .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "WebApiServerDemo.WebApi");
                    c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
                })
                .EnableSwaggerUi(c =>
                {
                    c.InjectJavaScript(typeof(WebApiServerDemoWebModule).Assembly, "WebApiServerDemo.Web.Api.Scripts.Swagger-Custom.js");
                });
        }
    }

Notice that a javascript file named Swagger-Custom.js is being injected. This file must be located in your Web project. For example:

Notice once again that filename (WebApiServerDemo.Web.Api.Scripts.Swagger-Custom.js) correspond with file path.

After that, the file Swagger-Custom.js must be settled as embedded resource and must be included in the AssemblyInfo.cs at your Web project.


//...
[assembly: WebResource("WebApiServerDemo.Web.Api.Scripts.Swagger-Custom.js", "application/x-javascript")]

And that's all! You can now explore your awesome API!

Optional references


Further Readings

Look at Swagger Code Generator for client-side code generation from your API.

Comments

  1. Thank you.
    I had a problem referencing the resource JS following this guide.
    I need to right click the project where is located the JS > Properties > Application and copy the "Default namespace" value.
    After that, supposing the JS file is in the folder "Scripts" the reference string should be:
    "DefaultNameSpace.Scripts.ReferencedJS.js"

    This is almost the same as your guide, but removing the "Web" part in the reference string.

    Best!

    ReplyDelete

Post a Comment

Popular posts from this blog

How to use Font Awesome with IText

This post is about on how to use Font Awesome with IText library to generate awesome icons in our PDF documents. In this post, we are using  ITextSharp , a C# port for IText, but those are the same functionalities.

TinyMCE: anti-forgery token failed on file upload in an ASP.NET Boilerplate application

TinyMCE is an excellent full featured and well documented web editor that we can use in our web applications. But, integration with ASP.NET MVC 5 (as part of the ASP.NET Boilerplate technology stack) may be tricky to accomplish. When you set up the file upload plugin in TinyMCE, you have to consider the ASP.NET MVC 5 anti-forgery system. Otherwise, HTTP requests may fail loading resources to server due to an invalid or empty anti-forgery token error. This is a security and technology concern that can run out your time if you don't know about it. This article explains how this mechanism works and shows some solutions in an ASP.NET Boilerplate application.