在asp.net core webapi 中开启swagger
2022/8/24 1:22:48
本文主要是介绍在asp.net core webapi 中开启swagger,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
首先需要安装包 Swashbuckle.AspNetCore
接着在项目中右键属性
接着在Startup 文件中声明一个字段
private string currentAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;
服务容器代码如下
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSwaggerGen(option => { option.SwaggerDoc(currentAssemblyName, new Microsoft.OpenApi.Models.OpenApiInfo { Version = "v1", Title = "在netcore3.1中,开启swagger", Description = "这是文档描述", Contact = new Microsoft.OpenApi.Models.OpenApiContact { Name = "syf", Email = "1010963984@qq.com" } }); option.IncludeXmlComments(System.IO.Path.Combine(AppContext.BaseDirectory, $"{currentAssemblyName}.xml"), true); }); }
管道中间件代码如下
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.UseSwagger(); app.UseSwaggerUI(option => { option.SwaggerEndpoint($"/swagger/{currentAssemblyName}/swagger.json", "接口文档"); option.DocumentTitle = "this is descrision"; }); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
接着修改launchSettings.json文件,将launchUrl修改为swagger
"openSwagger": { "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }
抑制警告: 在项目文件的xml中添加 <NoWarn>$(NoWarn);1591</NoWarn>
<PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> <GenerateDocumentationFile>True</GenerateDocumentationFile> <DocumentationFile>openSwagger.xml</DocumentationFile> <NoWarn>$(NoWarn);1591</NoWarn> </PropertyGroup>
更多配置内容可以参考官方文档
这篇关于在asp.net core webapi 中开启swagger的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#