0

I am trying to get Swagger documentation working for my ASP.NET Core application using Microsoft's aspnet-api-versioning and am following the instructions from here which seem to be out of date as I had to change things to make them compile.

However whenever I hit the /swagger end point I get the following error "Count not render e, see console":

enter image description here

I have configured things as follows:

public void ConfigureServices(IServiceCollection aServices)
{
    aServices.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    aServices.AddMvcCore().AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'VVV");
    aServices.AddApiVersioning();
    aServices.AddSwaggerGen(
        aOptions =>
        {
            IApiVersionDescriptionProvider provider = aServices.BuildServiceProvider()
                                                               .GetRequiredService<IApiVersionDescriptionProvider>();

            foreach (ApiVersionDescription description in provider.ApiVersionDescriptions)
            {
                aOptions.SwaggerDoc(
                    description.GroupName,
                    new Info
                    {
                        Title = $"Sample API {description.ApiVersion}",
                        Version = description.ApiVersion.ToString()
                    });
            }
        }
    );
}

public void Configure(IApplicationBuilder aApp, IHostingEnvironment aEnv)
{
    if (aEnv.IsDevelopment())
        aApp.UseDeveloperExceptionPage();
    else
        aApp.UseHsts();

    aApp.UseHttpsRedirection();
    aApp.UseMvc();
    aApp.UseSwagger();
    aApp.UseSwaggerUI();

    aApp.UseSwaggerUI(
        aOptions =>
        {
            IApiVersionDescriptionProvider provider = aApp.ApplicationServices.GetRequiredService<IApiVersionDescriptionProvider>();

            foreach (ApiVersionDescription description in provider.ApiVersionDescriptions)
            {
                aOptions.SwaggerEndpoint(
                    $"/swagger/{description.GroupName}/swagger.json",
                    description.GroupName.ToUpperInvariant());
            }
        }
    );
}

And then a very simple controller:

[Route("v{version:apiVersion}/api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    // DELETE api/values/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
    }

    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new[] {"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)
    {
    }
}

So it appears the error is missing the Url property. However I was expecting that to have come from the ASP.NET Api Versioning? What have I missed here and what do I need to do to make this work?

TheEdge
  • 9,291
  • 15
  • 67
  • 135
  • The setup looks correct. It appears that you are using ASP.NET 2.2, which is still in preview. This might be a bug in the preview. It's also possible that something isn't being setup correctly due to `ActionResult`. What happens if you remove the use of `ActionResult` and just return the response type? API versioning doesn't not officially support ASP.NET Core 2.2 - yet, but it should work with 2.1 compatibility or below enabled. – Chris Martinez Oct 25 '18 at 22:06
  • Were you ever able to resolve this? API Versioning officially supports 2.1 and 2.2 now. Based on the error location and message, this appears to be happening on the UI side of things. If you're following the examples from API versioning, then you're using Swashbuckle. Is there any chance there is/was a bug or change in the Swashbuckle UI scripts? – Chris Martinez Dec 23 '18 at 02:06

0 Answers0