gpt4 book ai didi

c# - Swagger Page 在 MVC net-core 中找不到 404

转载 作者:行者123 更新时间:2023-11-30 19:04:31 28 4
gpt4 key购买 nike

我有一个全新的 .NET-Core Web API 项目,我想使用 API 版本控制和 swagger。

当我尝试查看 swagger 页面时,我收到 404。但是,模板附带的默认 ValuesController 有效。

这是我的设置:

启动.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

[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)
{
}
}

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

<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>

有人看到我可能做错了什么吗?

最佳答案

当我创建项目时,我不小心将 .vs 文件夹包含在 checkin 中。当我从源代码管理中删除它时,无论出于何种原因,swagger 又开始工作了。

不确定解释是针对这个的。

关于c# - Swagger Page 在 MVC net-core 中找不到 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50379112/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com