Search code examples
.net-coreswaggerdocumentationswagger-uinswag

Unable to load Swagger API documentation with ODATA controller


We are trying to generate the swagger UI with OData and NSwag but we are ended up with lot of issues.
We are using OData controllers instead of ControllerBase

Startup file configuration

services.AddOData(); services.AddSwaggerDocument();

Startup Configure method

app.UseMvc(builder =>
{
builder.Select().Filter().Expand().MaxTop(1000).Count();
builder.MapODataServiceRoute("ODataRoutes", "api/v1", GetEdmModel(app.ApplicationServices));
builder.EnableDependencyInjection();
});
app.UseSwagger();
app.UseSwaggerUi3();

we are using values controller inheriting ODataController

[Route("api/[controller]")]
[ApiController]

public class ValuesController : Microsoft.AspNet.OData.ODataController
{
    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new string[] { "value1", "value2" };

    // GET api/values/5
    [HttpGet("{id}")]
    public ActionResult<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)
    {
    }
}

We getting error like " /swagger/{documentName}/swagger.json"
Are we missing something ?


Solution

  • We missed endpoint routing.

     services.AddMvc(options => { options.EnableEndpointRouting = false; }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);