. Net Core Tips-swagger adaptation virtual directories and level two directories

Source: Internet
Author: User

Objective

With the advent of front-end separation and microservices architectures, Web APIs become increasingly important and pervasive. The advent of Gateway technology has made developers more inclined to use level two/multilevel directories to expose Web APIs, with fewer ports to expose, easier to manage, and some public transactions, such as authentication, that can be handled in gateways. However, the swagger default is the appropriate root (root) directory, you want to adapt to the level two/multilevel directory, requires additional processing, but also to distinguish between the development environment and other environments. One way of thinking is to let the Web API be in the two level directory in the development environment, which can be simulated natively through Nginx and IIS virtual directories, but this idea is obviously a little cumbersome. Another way of thinking is to implement it by configuring a multi-environment.

Steps

1. Installing swagger

Install-package Swashbuckle.aspnetcore

2. Configuring the Swagger Service

Services. Addswaggergen (c +=    {c.swaggerdoc ("v1"new"My API  "v1 " } );

3. Configuring Swagger Middleware

var virtualpath = configuration["virtualpath"= =     > swaggerdoc.basepath == = ={    "/swagger/v1/swagger.json"  "My API V1");     string . Empty;});

4. Configure Appsettings.json

To add the configuration of virtualpath in Appsettings.json:

{  // Assuming a level two directory or virtual directory is/sg  "Logging":    {false,    " Debug ": {      " LogLevel ":{        " Default ":" Warning "      }    },    " Console ": {      " LogLevel ":{        " Default ":" Warning "}}}}  

In appsettings. Add the configuration of virtualpath in Development.json:

{  // using the root directory  ' Logging ':    {false,    ' LogLevel ' : {      "Default": "Debug",      "System": "Information",      "Microsoft": " Information "    }}  }

Test

1. Development environment Test

2. Simulated production environment test

Modify the Launchsettings.json to change the parameters of the environment variables:

Prepare the nginx.conf and start Nginx.

Worker_processes  1; events {    worker_connections  ;} HTTP {    include       mime.types;    Default_type  application/octet-stream;     sendfile on        ;    Keepalive_timeout  ;    server {        listen       ;        server_name  localhost;         /sg/ {            proxy_pass http://localhost:30925/;                       }    }}

Access Level Two directory

Summary

The code involved is not much, the main idea is to express the design ideas, as well as the multi-environmental treatment plan, it must be said that. Net core in the environmental adaptation and other aspects of the design is excellent.

The second-level catalog adaptation is a very small problem, but it is a very common problem in microservices practice.

Whether the adaptation of secondary catalogue can be automatically adapted rather than configured, is also a question worth thinking about.

Source

Https://github.com/ErikXu/.NetCoreTips/tree/master/SwaggerVirtualPath

. Net Core Tips-swagger adaptation virtual directories and level two directories

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.