ASP. NET Core WebApi uses swagger as a help document and customizes swagger UI

Source: Internet
Author: User

Webapi after writing, online Help documentation and tools to debug online are professional performance, and swagger is undoubtedly the best tool to do docs, automatically generate the interface description of each controller, automatically parse the parameters into JSON, and be able to debug online.

So what are the steps you need to apply to ASP. Swagger and how many pits to fill?

Install Swagger to Project
{    "dependencies": {                                "swashbuckle"  "6.0.0-beta902",                                ...

Or add swashbuckle directly through the NuGet interface, currently the latest version 6.0.0-beta902

Configure Swagger

1.startup.cs=>configureservices

//Document parsingServices. Addswaggergen ();//Non-essentialServices. Configureswaggergen (options ={options. Singleapiversion (NewInfo {Version="v1", Title="UFX. Mall Docking Enterprise Internal system service Middleware Interface Description document"+configuration.getvalue<string> ("Customer"), Description="Based on ASP webapi,powered by Pomelo van Information Technology www.cnunify.com"                }); });

2.startup.cs=>configure

// Document parsing             app. Useswagger ();            App. Useswaggerui ();

3. Description information of the automatic Read method

Reference Document: Https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger

Focus: How to customize the Swagger UI

After all the configuration has been done, direct access to the HTTP://XXX/SWAGGER/UI can see the interface interface.

But the default swagger UI personally think it is still a little ugly, some of the details are not in place, swagger all the resource files are embedded type, can not be directly modified, although a part of the UI interface, but how to fully customize the UI?

Swagger is a complete separation of the front and back of the project, the frontend static file through Ajax, request JSON data, return interface parsing display to the page, Swagger-ui can be found in git: https://github.com/swagger-api/swagger-ui/

Download the Swagger-ui to local and place all the files in the Dist wwwroot->swagger->ui

Then configure the real path for the ASP. NET core to automatically read wwwroot.

//This method gets called by the runtime. Use this method to configure the HTTP request pipeline         Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {Loggerf Actory. Addconsole (Configuration.getsection ("Logging"));            Loggerfactory.adddebug (); //Configure NlogLoggerfactory.addnlog (); Env. Configurenlog ("Nlog.config"); App.            Useapplicationinsightsrequesttelemetry (); App.            Useapplicationinsightsexceptiontelemetry (); //Exception Handling MiddlewareApp. Usemiddleware (typeof(Exceptionhandlermiddleware)); App.            Usemvc (); //Enable static files middleware.app.            Usestaticfiles (); App.            Usemvcwithdefaultroute (); //Document parsingapp.            Useswagger (); App.        Useswaggerui (); }

This way, all the swagger files are local and you can customize them to show the modified UI

When the WEBAPI is published to the server, the access to the lower right corner of the swagger will have an exception error, to cancel the error, you only need to add validatorurl index.html to null, cancel the authentication of the URL

            New Swaggerui ({                url:url,                null,                "Swagger-ui-container",

Reference Document: Http://stackoverflow.com/questions/27808804/swagger-ui-shows-error-validation-when-deployed

At the same time Swagger also provides an interface document editor Swagger-editor, you can easily edit the Swagger.json, edited well can be exported to the project

http://editor.swagger.io/

ASP. NET Core WebApi uses swagger as a help document and customizes swagger UI

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.