ABP框架系列之五十:(Swagger-UI-集成)
Introduction
From it's web site: "....with a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability."
从它的网站:“使API,你得到的交互式文档,客户端SDK的生成和发现。”
ASP.NET Core
Install Nuget Package
Install Swashbuckle.AspNetCore nuget package to your Web project.
Configure
Add configuration code for Swagger into ConfigureServices method of your Startup.cs
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//your other code... services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Info { Title = "AbpZeroTemplate API", Version = "v1" });
options.DocInclusionPredicate((docName, description) => true);
}); //your other code...
}
Then, add below code into Configure method of Startup.cs to use Swagger
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//your other code... app.UseSwagger();
//Enable middleware to serve swagger - ui assets(HTML, JS, CSS etc.)
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "AbpZeroTemplate API V1");
}); //URL: /swagger //your other code...
}
Test
That's all. You can browse swagger ui under "/swagger".
ASP.NET 5.x
Install Nuget Package
Install Swashbuckle.Core nuget package to your WebApi project (or Web project).
Configure
Add configuration code for Swagger into Initialize method of your module. Example:
public class SwaggerIntegrationDemoWebApiModule : AbpModule
{
public override void Initialize()
{
//your other code... ConfigureSwaggerUi();
} private void ConfigureSwaggerUi()
{
Configuration.Modules.AbpWebApi().HttpConfiguration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "SwaggerIntegrationDemo.WebApi");
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
})
.EnableSwaggerUi(c =>
{
c.InjectJavaScript(Assembly.GetAssembly(typeof(AbpProjectNameWebApiModule)), "AbpCompanyName.AbpProjectName.Api.Scripts.Swagger-Custom.js");
});
}
}
Notice that, we inject a javascript file named "Swagger-Custom.js" while configuring swagger ui. This script file is used to add CSRF token to requests while testing api services on swagger ui. You also need to add this file to your WebApi project as embedded resource and use it's Logical Name in InjectJavaScript method while injecting it.
IMPORTANT: The code above will be slightly different for your project (Namespace will not be AbpCompanyName.AbpProjectName... and AbpProjectNameWebApiModule will be YourProjectNameWebApiModule).
Content of the Swagger-Custom.js here:
var getCookieValue = function(key) {
var equalities = document.cookie.split('; ');
for (var i = 0; i < equalities.length; i++) {
if (!equalities[i]) {
continue;
}
var splitted = equalities[i].split('=');
if (splitted.length !== 2) {
continue;
}
if (decodeURIComponent(splitted[0]) === key) {
return decodeURIComponent(splitted[1] || '');
}
}
return null;
};
var csrfCookie = getCookieValue("XSRF-TOKEN");
var csrfCookieAuth = new SwaggerClient.ApiKeyAuthorization("X-XSRF-TOKEN", csrfCookie, "header");
swaggerUi.api.clientAuthorizations.add("X-XSRF-TOKEN", csrfCookieAuth);
See Swashbuckle documentation for more configuration options.
Test
That's all. Let's browse /swagger/ui/index:


You can see all Web API Controllers (and also dynamic web api controllers) and test them.
ABP框架系列之五十:(Swagger-UI-集成)的更多相关文章
- ABP框架系列之五十四:(XSRF-CSRF-Protection-跨站请求伪造保护)
Introduction "Cross-Site Request Forgery (CSRF) is a type of attack that occurs when a maliciou ...
- ABP框架系列之五十二:(Validating-Data-Transfer-Objects-验证数据传输对象)
Introduction to validation Inputs of an application should be validated first. This input can be sen ...
- ABP框架系列之三十四:(Multi-Tenancy-多租户)
What Is Multi Tenancy? "Software Multitenancy refers to a software architecture in which a sing ...
- ABP框架系列之四十:(Notification-System-通知系统)
Introduction Notifications are used to inform users on specific events in the system. ASP.NET Boiler ...
- ABP框架系列之十六:(Dapper-Integration-Dapper集成)
Introduction Dapper is an object-relational mapper (ORM) for .NET. Abp.Dapper package simply integra ...
- ABP框架系列之三十八:(NHibernate-Integration-NHibernate-集成)
ASP.NET Boilerplate can work with any O/RM framework. It has built-in integration with NHibernate. T ...
- ABP框架系列之三十九:(NLayer-Architecture-多层架构)
Introduction Layering of an application's codebase is a widely accepted technique to help reduce com ...
- ABP框架系列之四十九:(Startup-Configuration-启动配置)
ASP.NET Boilerplate provides an infrastructure and a model to configure it and modules on startup. A ...
- ABP框架系列之十五:(Caching-缓存)
Introduction ASP.NET Boilerplate provides an abstraction for caching. It internally uses this cache ...
随机推荐
- Core Graphices 设置渐变
Core Graphices 设置渐变 Quartz 提供了两种设置渐变的方式 CGShadingRef and CGGradientRef 尝试CGGradientRef 的使用 import & ...
- List 的add()与addAll()的区别
add 是将传入的参数作为当前List中的一个Item存储,即使你传入一个List也只会另当前的List增加1个元素addAll 是传入一个List,将此List中的所有元素加入到当前List中,也就 ...
- 解决Tomcat启动时项目重复加载问题
前几天一个同学项目要上线,部署到服务器时,因为客户需要通过IP直接可以访问到,所以在server.xml做了如下的配置 导致启动tomcat时候出现一个项目重复加载了两次,很容易就出现了内存溢出. 这 ...
- leetcode20:有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...
- Linux配置Supervisor 配置遇到的坑
在linux中web 应用部署到线上后之后发现退出终端后网站就无法访问了 所以需要用Supervisor来守护进程,它可以保证应用一直处于运行状态,在遇到程序异常.报错等情况,导致 web 应用终止时 ...
- 【学习】数据处理基础知识(基本功能)【pandas】
本章介绍pandas的重要功能,只记录一些重点内容 1.重新索引 pandas对象的一个重要方法是reindex,其作用是创建一个适应用新索引的新对象 #重新索引 obj = pd.Series([4 ...
- python_07 函数作用域、匿名函数
函数的作用域:无论在哪个地方调用函数,函数运行过程中的作用域只跟定义的时候有关,跟在哪个地方调用无关. name='alex' def foo(): name = 'linhaifeng' def b ...
- Itellj Idea使用技巧
模板设置: 类模板:File–>Settings–>Editor–>File and Code Templates–>Includes–>File Header 输入缩写 ...
- js:捕获冒泡和事件委托
一.事件流(捕获,冒泡) 事件流:指从页面中接收事件的顺序,有冒泡流和捕获流. 当页面中发生某种事件(比如鼠标点击,鼠标滑过等)时,毫无疑问子元素和父元素都会接收到该事件,可具体顺序是怎样的呢?冒 ...
- Mybatis-PageHelper分页插件
PageHelper.startPage 静态方法调用 除了 PageHelper.startPage 方法外,还提供了类似用法的 PageHelper.offsetPage 方法. 在你需要进行分页 ...