netcore3.0 webapi集成Swagger 5.0
1.netcore3.0 webapi集成Swagger 5.0
2.DingTalk钉钉消息推送(.net core 3 WebApi尝鲜记)3..net core 3.1 webapi后端接收钉钉小程序post的文件/图片4.asp.net core 3.1 webapi FileContentResult 5.一次asp.net core3.1打造webapi开发框架的实践6..net core3.1项目在centos7.6上部署经验7.ZR.Admin小改和VUE3版本体验8.我的微服务之路,看我搭建dapr趟过的坑9.42岁大龄程序员的迷茫,看我最新尝鲜.net 5+Dapper搭建的WebAPI框架10.Kestrel服务器ASP.NetCore 3.1程序启用SSL11.Asp.Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty(转)12.ApiResult-WebAPI开发统一返回值对象的演化(.net core版) 13.体验.net core 3.1 socket今天来尝尝鲜。貌似.net core 3.0使用Swagger 4.0.1会报错,随手一搜,还没人写这个把调试通过的代码贴一下:
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using System; using System.IO; using System.Reflection; namespace WebApplication4 { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = ".Net Core中间件API文档", Version = "v1" }); // 为 Swagger 设置xml文档注释路径 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //启用中间件服务生成Swagger app.UseSwagger(); //启用中间件服务生成SwaggerUI,指定Swagger JSON终结点 app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", ".Net Core中间件API文档 V1"); c.RoutePrefix = string.Empty;//设置根节点访问 }); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }
依赖包:
控制器:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace WebApplication4.Controllers { /// <summary> /// 天气预报API /// </summary> [ApiController] [Route("api/[controller]/[action]")] public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; private readonly ILogger<WeatherForecastController> _logger; public WeatherForecastController(ILogger<WeatherForecastController> logger) { _logger = logger; } /// <summary> /// 获取当前天气 /// </summary> /// <param name="size">城市的个数</param> /// <returns></returns> [HttpGet] [HttpPost] public IEnumerable<WeatherForecast> GetWeather(string size = "5") { var rng = new Random(); return Enumerable.Range(1, Convert.ToInt32(size)).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), Summary = Summaries[rng.Next(Summaries.Length)] }) .ToArray(); } /// <summary> /// 测试方法 /// </summary> /// <returns></returns> [HttpGet] public ApiResult Demo() { return new ApiResult { Message = "操作成功!", Success = true, Result = 1 }; } } }
作者:数据酷软件
出处:https://www.cnblogs.com/datacool/p/11805585.html
关于作者:20年编程从业经验,持续关注MES/ERP/POS/WMS/工业自动化
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
联系方式: qq:71008973;wx:6857740733
基于人脸识别的考勤系统 地址: https://gitee.com/afeng124/viewface_attendance_ext
自己开发安卓应用框架 地址: https://gitee.com/afeng124/android-app-frame
WPOS(warehouse+pos) 后台演示地址: http://47.239.106.75:8080/
合集:
.net core随笔
分类:
程序设计
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构