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

标签 c# api asp.net-core swagger

我有一个全新的 .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/

相关文章:

C# 初始化对象值

c# - 数组的值无缘无故地改变了

c# - 尝试将依赖项注入(inject)与 XUnit 一起使用时出现未解析的构造函数参数错误

C# 字符串比较 'ö' 'oe' 'o'

javascript - 如何使用 Node js 在airtable api 中进行异步等待工作,以在发送解析之前获取airtable 中的所有记录?

api - 访问localhost api时如何修复react-native中的网络错误

c# - 如何在 ASP.NET Core MVC 中获取 Url Referrer?

c# - 无法在服务中注入(inject) IOptions

javascript - 使用 Passport + Express 身份验证时,浏览器中不会存储 cookie

c# - 在 MVC 6 中使用 REST 服务