Skip to content

OData Function Versioning #201

Closed
Closed
@Zaheer-hawseea

Description

@Zaheer-hawseea

Hi,
I'm trying to version a function in OData whereby the function has different parameters depending on the version. However below error is obtained when HttpConfiguration.EnsureIsInitialized() is called. Your help will be greatly appreciated.

Error : The path template 'GetPerson(fname={fname}, lname={lname})' on the action 'GetPerson' in controller 'GetPerson' is not a valid OData path template. Resource not found for the segment 'GetPerson'.

Entity:

[DataContract]
public class Person
{
   [Key]
   [DataMember]
   public int Id { get; set; }

   [DataMember]
   public string FirstName { get; set; }

   [DataMember]
   public string LastName { get; set; }
}
public class PersonModelConfiguration : IModelConfiguration
    {
        public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
        {
            builder.EntitySet<Person>("Person");

            if (apiVersion.MajorVersion == 1)
            {
                var config1 = builder.Function("GetPerson");
                config1.Parameter<string>("id");
                config1.ReturnsFromEntitySet<Person>("Person");
            }
            else
            {
                var config2 = builder.Function("GetPerson");
                config2.Parameter<string>("fname");
                config2.Parameter<string>("lname");
                config2.ReturnsFromEntitySet<Person>("Person");
            }
        }
    }
public void Configuration(IAppBuilder app)
       {
           // Server configuration
           HttpConfiguration config = new HttpConfiguration();

           config.AddApiVersioning(o => {
               o.AssumeDefaultVersionWhenUnspecified = true;
               o.ReportApiVersions = true;
               o.ApiVersionReader = ApiVersionReader.Combine(
                       new QueryStringApiVersionReader(),
                       new HeaderApiVersionReader("api-version", "x-ms-version")
                   );
           });

           // OData Model Builder
           var builder = new VersionedODataModelBuilder(config)
           {
               ModelBuilderFactory = () => new ODataConventionModelBuilder().EnableLowerCamelCase(),
               ModelConfigurations = { new PersonModelConfiguration() }
           };

           var options = config.GetApiVersioningOptions();

           var v1 = new ApiVersion(1, 0);
           var v2 = new ApiVersion(2, 0);

           options.Conventions.Controller<GetPersonController>()
               .HasApiVersion(v1)
               .HasApiVersion(v2)
               .Action(a => a.GetPerson(default(string))).MapToApiVersion(v1)
               .Action(a => a.GetPerson(default(string), default(string))).MapToApiVersion(v2);

           var models = builder.GetEdmModels();

           // Map OData in OWin
           config.MapVersionedODataRoutes("Default-ODataRoute", "api", models);
           config.MapVersionedODataRoutes("ODataRoute-Versioned", "api/v{apiVersion}", models);

           config.EnsureInitialized();
           app.UseWebApi(config);
       }
public class GetPersonController : ODataController
   {
       private static Person testPerson1 = new Person() {Id = 1, FirstName = "James", LastName = "Brown"};
       private static Person testPerson2 = new Person() {Id = 2, FirstName = "Paul", LastName = "Allen"};

       [HttpGet]
       [ODataRoute("GetPerson(id={id})")]
       public async Task<IHttpActionResult> GetPerson([FromODataUri] string id)
       {
           var person = await Task.FromResult(testPerson1);

           return Ok(person);
       }

       [HttpGet]
       [ODataRoute("GetPerson(fname={fname}, lname={lname})")]
       public async Task<IHttpActionResult> GetPerson([FromODataUri] string fname, [FromODataUri] string lname)
       {
           var person = await Task.FromResult(testPerson2);

           return Ok(person);
       }
   }

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions