博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【swaggerui】swaggerui在asp.net web api core 中的应用
阅读量:7021 次
发布时间:2019-06-28

本文共 2178 字,大约阅读时间需要 7 分钟。

Swaggerui 可以为我们的webapi提供美观的在线文档,如下图:

 实现步骤:

  • NuGet Packages  Install-Package Swashbuckle.AspNetCore
  • 在startup文件中配置swagger

       

1            // Register the Swagger generator, defining one or more Swagger documents 2             services.AddSwaggerGen(c => 3             { 4                 c.SwaggerDoc("v1", new Info 5                 { 6                     Version = "v1", 7                     Title = "ToDo API", 8                     Description = "A simple example ASP.NET Core Web API", 9                     TermsOfService = "None",10                     Contact = new Contact { Name = "Shayne Boyer", Email = "", Url = "https://twitter.com/spboyer" },11                     License = new License { Name = "Use under LICX", Url = "https://example.com/license" }12                 });13 14                 //Set the comments path for the swagger json and ui.15                 var basePath = PlatformServices.Default.Application.ApplicationBasePath;16                 var xmlPath = Path.Combine(basePath, "MyWebApiCore.xml");17                 c.IncludeXmlComments(xmlPath);18             });19         }20 21         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.22         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)23         {24             loggerFactory.AddConsole(Configuration.GetSection("Logging"));25             loggerFactory.AddDebug();26 27             app.UseMvc();28             app.UseSwagger();29             app.UseSwaggerUI(c =>30             {31                 c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");32             });33         }

 

  • XML Comments,点击项目属性=》生成=》XML文档文件打勾,然后在你的action上添加注释

            /// <summary>

            /// Get方法无参数
            /// </summary>
            /// <returns>string[]数组</returns>
            [HttpGet]
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }

            /// <summary>

            /// 根据id获取
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            /// <remarks>
            /// Note that the id is an integer.
            /// </remarks>
            [HttpGet("{id}")]
            public string Get(int id)
            {
                return "value";
            }

  • 运行项目,输入文档地址http://localhost:58911/swagger/

    你可以选择方法进行在线测试

转载地址:http://mobxl.baihongyu.com/

你可能感兴趣的文章
变频电源具有的可靠性
查看>>
一些自己写的freemaker macro 用来生成网页中的区块
查看>>
法语Linux NuTyX 11 RC2 发布
查看>>
Java 集合系列01之 总体框架
查看>>
Visual Paradigm 教程[UML]:创建一个具有刻板印象的类
查看>>
安装flashplugin提示依赖libgdk-pixbuf2.0-0
查看>>
神奇的时间戳
查看>>
华为最新众包项目已发布,6万+项目费等你领
查看>>
停掉一台服务器,Nginx响应慢
查看>>
Install the python development environment
查看>>
2014-10-17(脚本练习)
查看>>
栈实现表达式求值
查看>>
Linux批量修改多台服务器的主机名(hostname)
查看>>
JVM 参数
查看>>
网络管理必备工具软件精解(Windows版)---转载及个人见解
查看>>
Status Text: BADRESPONSE: Unexpected token <
查看>>
java.lang.ClassNotFoundException: org.apache.St...
查看>>
linux shell脚本用while read逐行读取文本的问题
查看>>
Git使用过程中遇到的问题
查看>>
Python标准库学习笔记1:文本
查看>>