设为首页 加入收藏

TOP

Swagger如何访问Ocelot中带权限验证的API(二)
2019-09-17 18:09:44 】 浏览:57
Tags:Swagger 如何 访问 Ocelot 权限 验证 API
son
"; 35 }) 36 .UseSwaggerUI(options => 37 { 38 options.SwaggerEndpoint("/SwaggerAPI01/swagger.json", "API01"); 39 }); 40 }

SwaggerOcelot,Starup.cs配置

 1 public void ConfigureServices(IServiceCollection services)
 2 {
 3     services.AddOcelotJwtAuthorize();
 4     //注入Ocelot
 5     services.AddOcelot(Configuration);
 6     services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
 7  
 8     services.AddSwaggerGen(options =>
 9     {
10         options.SwaggerDoc("ApiGateway", new Info { Title = "网关服务", Version = "v1", Contact = new Contact { Email = "285130205@qq.com", Name = "SwaggerOcelot", Url = "http://10.10.10.10" }, Description = "网关平台" });
11     });
12 }
13  
14 public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
15 {
16     if (env.IsDevelopment())
17     {
18         app.UseDeveloperExceptionPage();
19     }
20  
21     var apis = new Dictionary<string, string>(
22         new KeyValuePair<string, string>[] {
23             KeyValuePair.Create("SwaggerAuthorize", "Authorize"),
24             KeyValuePair.Create("SwaggerAPI01", "API01"),
25             KeyValuePair.Create("SwaggerAPI02", "API02")
26         });
27  
28     app.UseMvc()
29        .UseSwagger()
30        .UseSwaggerUI(options =>
31        {
32            apis.Keys.ToList().ForEach(key =>
33            {
34                options.SwaggerEndpoint($"/{key}/swagger.json", $"{apis[key]} -【{key}】");
35            });
36            options.DocumentTitle = "Swagger测试平台";
37        });
38     await app.UseOcelot();
39 }

接下来,为Swagger访问Web API项目,添加请求返回格式,默认状况下,Swagger是支持Json的,下来添加支持XML格式

第一步,添加支持XML格式

1 services.AddMvc()
2                   .AddXmlSerializerFormatters() //设置支持XML格式输入输出
3                   .AddJsonOptions(op => op.SerializerSettings.ContractResolver = new DefaultContractResolver())//大小写不转换
4                   .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

第二步,在对应的Action添加ProducesResponseType特性,为转换作支持

1 [HttpGet("{id}")]
2 [ProducesResponseType(typeof(API01Model), 200)]
3 public ActionResult<API01Model> Get(int id)
4 {
5     return new API01Model { ID = 1, IsSure = true, Price = 2.3m, Describe = "test1" };
6 }

运行效果:

先看登录

 

再看api访问

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Ocelot.JwtAuthorize:一个基于网.. 下一篇入门设计模式之适配器模式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目