Swagger Page is 404 Not Found in MVC net-core

17,010

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.

Not sure that the explanation is for this.

Share:
17,010

Related videos on Youtube

Tyler Findlay
Author by

Tyler Findlay

Lover of .NET, WordPress, Machine Learning, and all JavaScript frameworks. Coder by day, entrepreneur by night. Would make an outstanding Jedi.

Updated on June 08, 2022

Comments

  • Tyler Findlay
    Tyler Findlay almost 2 years

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

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

    Here is my setup:

    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

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

    • R. Richards
      R. Richards almost 6 years
      Did you check the XML documentation file option in the Output section on the Build tab of the project properties?
    • Utkarsh Bais
      Utkarsh Bais
      Try setting up the root url for swagger
  • IfElseTryCatch
    IfElseTryCatch over 5 years
    Mine randomly started working again when I deleted the .vs file from the solution in explorer. Although it didn't help that I was getting an error from one of my controllers... I think that may have been the issue...
  • Fernando
    Fernando about 5 years
    Mine as well, it simply started working after deleting the .vs folder. This was driving me crazy.
  • Manfred Wippel
    Manfred Wippel about 5 years
    Thank you so much. This helped a lot Every time i set up a project there is something different with the swagger UI
  • JensB
    JensB about 5 years
    Just ran into this now. Was working perfectly, then all of a sudden it gave 404. Closed VS2017 and removed /.vs folder. Now it works again.
  • Karel Kral
    Karel Kral almost 3 years
    I have run into the same issue with VS 2019. Deleting .vs folder resolves the issue. Unbelievable.