& Lt; ABP framework & gt; Swagger UI integration, abpswagger

Source: Internet
Author: User

<ABP framework> Swagger UI integration, abpswagger

Document directory

 

Content of this section:

  • Introduction
  • Asp.net Core
    • Install
    • Install the Nuget package
    • Configuration
    • Test
  • Asp.net 5.x
    • Install
    • Install the Nuget package
    • Configuration
    • Test

 

Introduction

From its webpage: "... using a Swagger-enabled Api, you will get the interaction document, create and expose the client SDK .".

 

Asp.net Core

Install

Based on the Asp.net Core application, you can easily integrate Swagger into your ABP.

 

Install the Nuget package

Install the Swashbuckle package in your Web project.

 

Configuration

In your Startup. cs file, add code to the ConfigureServices method and configure Swagger:

public IServiceProvider ConfigureServices(IServiceCollection services){    //your other code...        services.AddSwaggerGen();        //your other code...}

To use Swagger, add the following code in the Configure method in the Startup. cs file:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){    //your other code...    app.UseSwagger();    app.UseSwaggerUi(); //URL: /swagger/ui}

Finally, when testing the dynamic Web Api service from the Swagger UI, to send the CSRF Token, you need to add the Swagger UI index.html file to your project, which should be placed in the"Wwwroot \ swagger \ ui"Folder, then you can modify the onComplete method of Swagger UI in index.html, as shown below:

onComplete: function(swaggerApi, swaggerUi){  if(typeof initOAuth == "function") {    initOAuth({      clientId: "your-client-id",      clientSecret: "your-client-secret-if-required",      realm: "your-realms",      appName: "your-app-name",      scopeSeparator: " ",      additionalQueryStringParams: {}    });  }  if(window.SwaggerTranslator) {    window.SwaggerTranslator.translate();  }  var csrfToken = abp.security.antiForgery.getToken();  var csrfCookieAuth = new SwaggerClient.ApiKeyAuthorization(abp.security.antiForgery.tokenHeaderName, csrfToken, "header");  swaggerUi.api.clientAuthorizations.add(abp.security.antiForgery.tokenHeaderName, csrfCookieAuth);}

View the Swashbuckle document to obtain more configuration options.

 

Test

Now, you can browse the Swagger UI:"/Swagger/ui/index".

 

Asp.net 5.x

Install

Based on applications, you can easily integrate Swagger into your ABP.

 

Install the Nuget package

Install the Swashbuckle. core Package in your WebApi project (or Web project) (kid1412 note: this should be the Swashbuckle package, and the Swashbuckle. Core package should be installed in the Asp.net Core application ).

 

Configuration

Add the configuration code for Swagger to the Initialize method of your module. For example:

public class SwaggerIntegrationDemoWebApiModule : AbpModule{    public override void Initialize()    {        //your other code...        ConfigureSwaggerUi();    }    private void ConfigureSwaggerUi()    {        Configuration.Modules.AbpWebApi().HttpConfiguration            .EnableSwagger(c =>            {                c.SingleApiVersion("v1", "SwaggerIntegrationDemo.WebApi");                c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());            })            .EnableSwaggerUi(c =>            {                c.InjectJavaScript(Assembly.GetAssembly(typeof(AbpProjectNameWebApiModule)), "AbpCompanyName.AbpProjectName.Api.Scripts.Swagger-Custom.js");            });    }}

 

Note: When configuring the Swagger ui, we inject a javascript file named "Swagger-Custom.js" that adds a CSRF token when testing the api service from the Swagger ui, at the same time, you also need to add this file in your WebApi project. When injecting it, use its logical name to inject javascript methods. Its content is as follows:

var getCookieValue = function(key) {    var equalities = document.cookie.split('; ');    for (var i = 0; i < equalities.length; i++) {        if (!equalities[i]) {            continue;        }        var splitted = equalities[i].split('=');        if (splitted.length !== 2) {            continue;        }        if (decodeURIComponent(splitted[0]) === key) {            return decodeURIComponent(splitted[1] || '');        }    }    return null;};var csrfCookie = getCookieValue("XSRF-TOKEN");var csrfCookieAuth = new SwaggerClient.ApiKeyAuthorization("X-XSRF-TOKEN", csrfCookie, "header");swaggerUi.api.clientAuthorizations.add("X-XSRF-TOKEN", csrfCookieAuth);

 

View the Swashbuckle document to obtain more configuration options.

 

Test

Now, you can browse the Swagger UI:"/Swagger/ui/index".

You can see all the Web Api controllers (as well as dynamic Web Api controllers) and test them.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.