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

Tyl*_*lay 2 c# api swagger asp.net-core

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

当我尝试查看摇摇欲坠的页面时,我得到404。但是,模板随附的默认ValuesController有效。

这是我的设置:

启动文件

    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"); });
    }
Run Code Online (Sandbox Code Playgroud)

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)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

Tyl*_*lay 12

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

不确定解释是否为此。

  • 当我从资源管理器的解决方案中删除 .vs 文件时,我的随机再次开始工作。虽然我从我的一个控制器那里收到错误并没有帮助......我认为这可能是问题所在...... (3认同)
  • 我的也是,它只是在删除.vs文件夹后才开始工作。这让我发疯。 (3认同)
  • 非常感谢。这很有帮助每次我建立一个项目时,swagger UI 都会有一些不同 (2认同)
  • 现在刚遇到这个。工作正常,然后突然出现 404。关闭 VS2017 并删除 /.vs 文件夹。现在它又工作了。 (2认同)
  • 我在 VS 2019 中遇到了同样的问题。删除 .vs 文件夹可以解决该问题。难以置信。 (2认同)