Swagger 和 .Net Core 3 集成

编程入门 行业动态 更新时间:2024-10-23 23:32:54
本文介绍了Swagger 和 .Net Core 3 集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 swagger 记录我的 web api.但是,当我运行我的项目时,它会引发错误找不到方法:'Void Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware...".

我只遵循了 youtube 上的教程.不确定错误的原因是什么.

我的项目是 .Net Core 3.

内部类 SwaggerConfiguration{公共静态无效配置(IServiceCollection 服务){services.AddSwaggerGen(c =>{c.SwaggerDoc("v1", new Info() { Title = "Sample Portal API", Version = "1.0.0.0" });});}公共静态无效配置(IConfiguration配置,IApplicationBuilder应用程序){var swaggerOptions = new SwaggerOptions(configuration).Bind();app.UseSwagger(options =>{options.RouteTemplate = swaggerOptions.Route;});app.UseSwaggerUI(options =>{options.SwaggerEndpoint(swaggerOptions.UIEndpoint, swaggerOptions.Description);});}私有类 SwaggerOptions{IConfiguration_configuration;公共 SwaggerOptions(IConfiguration 配置){_configuration = 配置;}公共字符串路由{获取;放;}公共字符串 描述 { get;放;}公共字符串 UIEndpoint { 获取;放;}公共 SwaggerOptions 绑定(){_configuration.GetSection(nameof(SwaggerOptions)).Bind(this);返回这个;}}}

这是我的创业班

public void ConfigureServices(IServiceCollection services){services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);SwaggerConfiguration.Configure(services);}公共无效配置(IApplicationBuilder 应用程序,IWebHostEnvironment 环境){如果 (env.IsDevelopment()){app.UseDeveloperExceptionPage();}SwaggerConfiguration.Configure(Configuration, app);app.UseHttpsRedirection();app.UseRouting();app.UseAuthorization();app.UseEndpoints(endpoints =>{端点.MapControllers();});}

有人可以照亮,提前致谢

解决方案

我可以通过点击此链接(在撰写本文时于 2019 年 8 月 20 日更新)::>

开始使用 Swashbuckle 和 ASP.NET Core

也看看他们的 GitHub 链接:

AspNetCore.Docs

I'm documenting my web api using swagger. However when I run my Project, it throws an error "Method not found: 'Void Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware...".

I only followed the tutorials from youtube. Not sure what is the cause of error.

My project is .Net Core 3.

internal class SwaggerConfiguration { public static void Configure(IServiceCollection services) { services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info() { Title = "Sample Portal API", Version = "1.0.0.0" }); }); } public static void Configure(IConfiguration configuration, IApplicationBuilder app) { var swaggerOptions = new SwaggerOptions(configuration).Bind(); app.UseSwagger(options => { options.RouteTemplate = swaggerOptions.Route; }); app.UseSwaggerUI(options => { options.SwaggerEndpoint(swaggerOptions.UIEndpoint, swaggerOptions.Description); }); } private class SwaggerOptions { IConfiguration _configuration; public SwaggerOptions(IConfiguration configuration) { _configuration = configuration; } public string Route { get; set; } public string Description { get; set; } public string UIEndpoint { get; set; } public SwaggerOptions Bind() { _configuration.GetSection(nameof(SwaggerOptions)).Bind(this); return this; } } }

this is my startup class

public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); SwaggerConfiguration.Configure(services); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } SwaggerConfiguration.Configure(Configuration, app); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }

Can someone shed a light, thanks in advance

解决方案

I was able to get it going by following this link (updated 8/20/2019 as of this writing):

Get started with Swashbuckle and ASP.NET Core

Take a look at their GitHub link too:

AspNetCore.Docs

更多推荐

Swagger 和 .Net Core 3 集成

本文发布于:2023-11-15 19:06:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1597164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Swagger   Net   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!