这是我的
Startup.cs
文件
这是我在 Startup.cs 中的
ConfigureService
方法。我已经按照文档完全修改了它,但它不起作用。我已经删除了启动 Url,因此它只是在端口上运行,并且我没有设置任何路由。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddControllers();
services.ConnectionToACQEs(Configuration);
services.AddAutoMapper(typeof(Startup));
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "ToDo API",
Description = "A simple example ASP.NET Core Web API",
TermsOfService = new Uri("https://example.com/terms"),
Contact = new OpenApiContact
{
Name = "Nicky Liu",
Email = "[email protected]",
Url = new Uri("https://www.zedotech.com"),
},
License = new OpenApiLicense
{
Name = "Use under LICX",
Url = new Uri("https://example.com/license"),
}
});
});
}
这是我的
Configure
方法:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//if (env.IsDevelopment())
//{
// app.UseDeveloperExceptionPage();
//}
/// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
//app.UseHttpsRedirection();
app.UseRouting();
//app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
尝试清理你的构建并构建你的项目。
如果还是不行,请确保你的控制器没有没有ActionVerbs的方法,即:
HttpGet, HttpPost
等等。 Swagger 要求所有控制器方法都具有 ActionVerbs,除了那些具有 [ApiExplorerSettings(IgnoreApi = true)]
属性的方法。
您的任何方法都不应与 ActionVerb 一起定义路线: 做
[HttpGet,Route("getuser")]
而不是 [HttpGet("getuser")]
就我而言,@Sydney_dev 的答案不起作用。删除
RoutePrefix
设置后它就起作用了,但让我解释一下。
SwaggerUI 可通过类似于 https://localhost:5826/[RoutePrefix] 的 URL 访问。访问时,它会返回一个“永久”重定向到您可以测试 API 的页面。此永久重定向存储在浏览器中,即使 RoutePrefix
更改也有效。这可能最终会重定向并显示一个带有 HTTP 状态 404(未找到)的空页面。
中搜索。就我而言,指定了以下设置:
"launchUrl": "swagger"
这意味着当应用程序开始调试时,它会打开一个类似于https://localhost:5826/swagger
的 URL。当 RoutePrefix
设置为空时,SwaggerUI 不位于此处。要么更改
launchUrl
,要么删除 RoutePrefix
,要么在启动调试器后更改 URL。