惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
Microsoft Azure Blog
Microsoft Azure Blog
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
博客园_首页
月光博客
月光博客
N
News | PayPal Newsroom
The Cloudflare Blog
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
量子位
G
Google Developers Blog
T
Troy Hunt's Blog
博客园 - Franky
腾讯CDC
S
Security Affairs
J
Java Code Geeks
aimingoo的专栏
aimingoo的专栏
S
Security @ Cisco Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
D
DataBreaches.Net
Recorded Future
Recorded Future
H
Heimdal Security Blog
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
D
Docker
P
Proofpoint News Feed
V
V2EX
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Secure Thoughts
Engineering at Meta
Engineering at Meta
PCI Perspectives
PCI Perspectives
宝玉的分享
宝玉的分享
The Hacker News
The Hacker News
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cloudbric
Cloudbric
Microsoft Security Blog
Microsoft Security Blog
G
GRAHAM CLULEY
MyScale Blog
MyScale Blog
L
LINUX DO - 热门话题
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary

博客园 - 魏巍(QQ:68235081)

.Net Core实战教程(三):使用Supervisor配置守护进程 .Net Core实战教程(二):设置Kestrel的IP与端口的几种方法 .Net Core实战教程(一):Linux下搭建项目 关于.Net Core 前后端分离跨域请求时 ajax并发请求导致部分无法通过验证解决办法。 windows 2012 IIS 部署 .net core HTTP Error 502.5 - Process Failure 错误解决办法 sqlserver 删除所有记录后,让自增列从1开始 .Net Core 给WebApi接口返回值添加全局的日期格式化 Message "'OFFSET' 附近有语法错误。 在 FETCH 语句中选项 NEXT 的用法无效。" 解决办法 EntityFrameworkCore 关于.Net Core 部署在Linux下连接SqlServer数据库超时解决办法 EntityFrameworkCore Db First 生成Model时出错 PowerShell 版本过低 vs2017添加引用提示“找不到 Microsoft.VisualStudio.Shell.Interop.IVsReferenceManager 服务的实例”解决方案 发布mvc遇到的HTTP错误 403.14-Forbidden解决办法 如何在.Net Mvc中让Get,Post请求访问同一个Action的方法 jquery中attr和prop的区别 WebApi验证 解决Code First MySql数据库 Specified key was too long; max key length is 767 bytes异常 【转】Unobtrusive Ajax的使用 jquery.validate.unobtrusive的使用 企业库判断数据库连接类型
在.Net Core中使用Swagger制作接口文档
魏巍(QQ:68235081) · 2017-07-12 · via 博客园 - 魏巍(QQ:68235081)

在实际开发过程中后台开发人员与前端(移动端)接口的交流会很频繁。所以需要一个简单的接口文档让双方可以快速定位到问题所在。

Swagger可以当接口调试工具也可以作为简单的接口文档使用。

在传统的asp.net开发WebApi的过程中大家对Swagger应该都会很熟悉。

接下来我为大家带来如何在.Net Core中搭建一个Swagger环境。

效果图:

1.首先我们在NuGet中搜索“Swashbuckle.AspNetCore”然后安装。

2.生成XML文档:

在工程文件上右键->属性->左边选择“生成”->勾中“XML documentation file”选项->保存

完成后重新生成项目会在“bin\Debug\netcoreapp1.0”下面发现一个与工程名称一样的xml文件。

3.在“Startup”类中加入下面代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.PlatformAbstractions;
using Swashbuckle.AspNetCore.Swagger;

namespace online.web
{
    /// <summary>
    /// 
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="env"></param>
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            Configuration = builder.Build();
        }

        /// <summary>
        /// 
        /// </summary>
        public IConfigurationRoot Configuration { get; }

        /// <summary>
        /// 服务
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1",
                    new Info
                    {
                        Title = "广州树童英语API接口文档",
                        Version = "v1",
                        Description = "限用于开发接口调试.",
                        Contact = new Contact
                        {
                            Name = "魏巍",
                            Email = "68235081@qq.com"
                        }
                    }
                 );

                var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "online.web.xml");
                c.IncludeXmlComments(filePath);
            });
        }

        /// <summary>
        /// 中间件
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        /// <param name="loggerFactory"></param>
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            app.UseMiddleware<Middlewares.ExceptionHandlerMiddleware>();

            app.UseMvc();

            app.UseSwagger(c =>
            {
                c.RouteTemplate = "doc/{documentName}/swagger.json";
            });

            app.UseSwaggerUI(c =>
            {
                c.RoutePrefix = "doc";
                c.SwaggerEndpoint("/doc/v1/swagger.json", "API v1");
            });
        }
    }
}

有些与Swagger无关的代码可以删掉。

成功以后访问:“http://192.168.3.190:9000/doc/”

这里我为了访问地址方便把swagger与成了doc。大家也可以换成自己习惯的访问地址。

更多使用方法请看Swashbuckle.AspNetCore类库的地址:https://github.com/domaindrivendev/Swashbuckle.AspNetCore