OData / OData/AspNetCoreOData

UnsupportedApiVersion error

Open
#370 4 comments 0 reactions 1 assignee View on GitHub

@xuzhg is already working on this.

Since Nov 16, 2021.

followup
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

Steps to reproduce:

  1. Create Asp.Net Core Web API project
  2. This project must have 3 NuGet packages
  • Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer
  • Swashbuckle.AspNetCore
  • Microsoft.AspNetCore.OData 8.0.4
  1. Create class
public class TestModel
{
    public string Id { get; set; }
    public string Summary { get; set; }
}
  1. Startup class will look like:
public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddOData(options =>
            {
                options.Select().Count().SkipToken().Expand().SetMaxTop(10);
                options.AddRouteComponents("api/v{version:apiVersion}", BuildEdmModel());
            });

            services.AddApiVersioning(options => options.AssumeDefaultVersionWhenUnspecified = false);
            services.AddVersionedApiExplorer(options => options.SubstituteApiVersionInUrl = true);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "AspNetCoreTest1", Version = "v1" });
            });

            
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "AspNetCoreTest1 v1"));
            }

            app.UseODataRouteDebug();

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

        private static IEdmModel BuildEdmModel()
        {
            var builder = new ODataConventionModelBuilder();

            builder.EntitySet<TestModel>(nameof(TestModel));

            return builder.GetEdmModel();
        }
    }
  1. Add controller
[ApiController]
    [ApiVersion("1.0")]
    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.Select | AllowedQueryOptions.Count | AllowedQueryOptions.Skip | AllowedQueryOptions.Top | AllowedQueryOptions.Expand)]
    [Route("api/v{version:apiVersion}")]
    public class WeatherForecastController : ODataController
    {
        [HttpGet("TestModel({id})")]
        public IActionResult Get(string id)
        {
            var testModel = new TestModel
            {
                Id = id,
                Summary = "Summary"
            };

            return Ok(testModel);
        }
    }
  1. Start application and then type in browser something like
    https:/api/v1/TestModel('id')

OData response will be like
image

So far so good. Then try to navigate to @odata.context url. You should see OData metadata. Instead you have
image

Some additional observations:

  • If you change [HttpGet("TestModel({id})")] with [HttpGet("TestModel/{id}")] code works fine. But as I understand both formats are valid, right?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.