系列目录

循序渐进学.Net Core Web Api开发系列目录

本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi

一、概述

既然前后端开发完全分离,那么接口的测试和文档就显得非常重要,文档维护是一件比较麻烦的事情,特别是变更的文档,这时采用Swagger就会非常方便,同时解决了测试和接口文档两个问题。

二、使用NuGet获取包

使用NuGet搜索包:Swashbuckle.aspnetcore并安装。

三、添加代码

在Startup类的ConfigureServices方法内添加下面代码(加粗部分)

      public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); services.AddSwaggerGen(option =>
{
option.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "SaleService接口文档",
Description = "RESTful API for SaleService.",
TermsOfService = "None",
Contact = new Contact { Name = "seabluescn", Email = "seabluescn@163.com", Url = "" }
}); //Set the comments path for the swagger json and ui.
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var xmlPath = Path.Combine(basePath, "SaleService.xml"
);
option.IncludeXmlComments(xmlPath);
});

}

在Startup类的Configure方法内添加下面代码(加粗部分)

       public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvcWithDefaultRoute(); app.UseSwagger();
app.UseSwaggerUI(option
=>
{
option.ShowExtensions();
option.SwaggerEndpoint("/swagger/v1/swagger.json", "SaleService V1"
);
});

}

四、配置

需要在生成配置选项内勾选XML文档文件,项目编译时会生成该文件。Debug和Release模式都设置一下。否则会报一个System.IO.FileNotFoundException的错误。

五、启动

启动项目,浏览器输入:http://localhost:50793/swagger/ 即可。

也可以修改launchsettings.json文件,默认项目启动时运行swagger

{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50792/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SaleService": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:50793/"
}
}}

如果你对你的控制器方法进行了注释,swagger接口页面就会体现出来。

        /// <summary>
/// 根据产品编号查询产品信息
/// </summary>
/// <param name="code">产品编码</param>
/// <returns>产品信息</returns>

[HttpGet("{code}")]
public Product GetProduct(String code)
{
var product = new Product
{
ProductCode = code,
ProductName = "啫喱水"
}; return product;
}

下面为Swagger的首页:可以用它进行API的调试了。

循序渐进学.Net Core Web Api开发系列【2】:利用Swagger调试WebApi的更多相关文章

  1. 循序渐进学.Net Core Web Api开发系列【3】:WebApi开发概览

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 目前我们已 ...

  2. 循序渐进学.Net Core Web Api开发系列【0】:序言与目录

    一.序言 我大约在2003年时候开始接触到.NET,最初在.NET framework 1.1版本下写过代码,曾经做过WinForm和ASP.NET开发.大约在2010年的时候转型JAVA环境,这么多 ...

  3. 【目录】循序渐进学.Net Core Web Api开发系列

    当前标签: 循序渐进学.Net Core Web Api开发系列   循序渐进学.Net Core Web Api开发系列[16]:应用安全续-加密与解密 NET未来之路 2019-03-13 15: ...

  4. 循序渐进学.Net Core Web Api开发系列【16】:应用安全续-加密与解密

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 应用安全除 ...

  5. 循序渐进学.Net Core Web Api开发系列【15】:应用安全

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍W ...

  6. 循序渐进学.Net Core Web Api开发系列【14】:异常处理

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍异 ...

  7. 循序渐进学.Net Core Web Api开发系列【13】:中间件(Middleware)

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...

  8. 循序渐进学.Net Core Web Api开发系列【12】:缓存

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...

  9. 循序渐进学.Net Core Web Api开发系列【11】:依赖注入

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍如 ...

  10. 循序渐进学.Net Core Web Api开发系列【10】:使用日志

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.本篇概述 本篇介 ...

随机推荐

  1. 调试技巧 ------ printf 的使用技巧

    编译器宏:__FUNCTION__,__FILE__,__LINE__ #define __debug #ifdef __debug //#define debug(format,...) print ...

  2. NodeJS 笔记 URL模块

    url模块 ,包含分析和解析 URL 的工具. var url = require('url'); url.parse(urlStr[, parseQueryString][, slashesDeno ...

  3. 质数——1到n遍历法

    一.从1至N全部遍历,当这个数只能被1和n整除它就是素数. /** * 打印自然数n以内的素数 */ public void printPrime(int n){ //是否为质数 boolean is ...

  4. 读asyncio模块源码时的知识补漏

    硬着头皮看了一周的asyncio模块代码,了解了大概的执行流程,引用太多,成尤其是对象间函数的引用. 光是这么一段简单的代码: # coding: utf8 import asyncio import ...

  5. 浅谈 JSON 那些被转义的字符们

    其实,之前我一直以为 JSON 会把 ASCII 可显示字符以外的统统转义为 Unicode,直到有一次我用 JSON.stringify 才发现,其实是 PHP 为我们想的太周到了. 我以前是一位 ...

  6. RaspberryPi.1.开机与远程桌面

    raspberry  3b+ ------------------------------------------------------------------------------- 写系统 有 ...

  7. Python实现 -- 冒泡排序、选择排序、插入排序

    冒泡排序 冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法. 冒泡排序的原理: 比较两个相邻的元素,如果第一个比第二个大,就交换他们 对每一对相邻的元素做同样的工作,从开始第 ...

  8. linux使用badblocks命令扫描硬盘排除故障(待验证)

    检查硬盘是否产生坏道并输出# badblocks -s -v -o /root/badblocks.log /dev/sda              //公司操作 -s     Show the p ...

  9. Jenkins与代码上线解决方案

    Jenkins是一个用Java编写的开源的持续集成工具.在与Oracle发生争执后,项目从Hudson项目独立. Jenkins提供了软件开发的持续集成服务.它运行在Servlet容器中(例如Apac ...

  10. ecplise里的run as里只有run configurations是怎么回事?

    一.没有main方法 二.main方法所在的类不是在与文件名同名的类中