在MVC网络核心中找不到Swagger页面404

编程入门 行业动态 更新时间:2024-10-28 12:22:57
本文介绍了在MVC网络核心中找不到Swagger页面404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个全新的.NET-Core Web API项目,我想使用API​​版本控制和扩展功能.

I have a brand new .NET-Core Web API project that I want to use API versioning and swagger.

当我尝试查看摇摇欲坠的页面时,我得到了404.但是,模板随附的默认ValuesController起作用.

When I try to view the swagger page I get a 404. However, the default ValuesController that comes with the template works.

这是我的设置:

Startup.cs

Startup.cs

public void ConfigureServices(IServiceCollection services) { services.AddMvc(); // Add API Versioning services.AddApiVersioning( options => { options.DefaultApiVersion = new ApiVersion(1, 0); options.AssumeDefaultVersionWhenUnspecified = true; options.ReportApiVersions = true; }); // Add Swagger string pathToDoc = "RegistriesApi.xml"; services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Info { Title = "Registries API", Version = "v1", Description = "A simple api to interact with AMA registries information", TermsOfService = "None" }); string filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, pathToDoc); options.IncludeXmlComments(filePath); options.DescribeAllEnumsAsStrings(); }); } ... public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // Url Path Rewriter RewriteOptions rewriteOptions = new RewriteOptions(); if (!env.IsDevelopment()) { rewriteOptions.AddRedirectToHttps(); } else { app.UseDeveloperExceptionPage(); } app.UseRewriter(rewriteOptions); // Use MVC app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); // Swagger app.UseSwagger(c => { c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value); }); app.UseSwaggerUI( c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs"); }); }

ValuesController.cs

ValuesController.cs

[ApiVersion("1.0")] [Route("api/v{version:apiVersion}/values")] public class ValuesController : Controller { // GET api/values [HttpGet] public IEnumerable<string> Get() { return new[] {"value1", "value2"}; } // GET api/values/5 [HttpGet("{id}")] public string Get(int id) { return "value"; } // POST api/values [HttpPost] public void Post([FromBody] string value) { } // PUT api/values/5 [HttpPut("{id}")] public void Put(int id, [FromBody] string value) { } // DELETE api/values/5 [HttpDelete("{id}")] public void Delete(int id) { } }

此外,这是我已安装的所有库的版本:

Also, here is the version for all the libraries I have installed:

<ItemGroup> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="2.2.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="2.4.0" /> </ItemGroup>

有人看到我在做什么错吗?

Does anyone see what I might be doing wrong?

推荐答案

创建项目时,我不小心将.vs文件夹包括在签入中.当我从源代码管理中删除该文件夹时,无论出于什么原因,昂首阔步地重新开始工作.

When I created the project, I accidentally included the .vs folder in check in. When I removed it from source control, for whatever reason swagger started working again.

不确定解释是否为此.

更多推荐

在MVC网络核心中找不到Swagger页面404

本文发布于:2023-11-09 17:03:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572932.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中找   核心   页面   网络   MVC

发布评论

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

>www.elefans.com

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