scottsanpedro
scottsanpedro

Reputation: 1192

AutoRest Targeting web api version

I have not implemented versioning to my api yet, as wanted to get some more info first. I am using asp.net web api, and integrating into a WPF application, using AutoRest. Its been running for some months now, but I'm looking to use versioning with the api.

With a typical call from WPF to the api, is there a way to target particular versions of the api?

public async Task<ObservableCollection<EventsDTO>> GetEvents(bool ShowInActive)
    {
        try
        {
            CheckCredentials.CheckValidCredentials();
            using (var db = new BuxtedAPI(CheckCredentials.RestCredentials))
            {       
                var res = await db.GetEventsAsync(ShowInActive).ConfigureAwait(false);
                var obs = new ObservableCollection<EventsDTO>(res);
                return obs;
            }
        }
        catch (Exception ex)
        {
            logger.Error(ex);               
            return null;
        }
    }

Thanks in advance.

Upvotes: 0

Views: 146

Answers (1)

scottsanpedro
scottsanpedro

Reputation: 1192

If anyone else has this problem.

 public async Task<ObservableCollection<EventsDTO>> GetEvents(bool ShowInActive)
    {
        try
        {
            CheckCredentials.CheckValidCredentials();
            using (var db = new BuxtedAPI(CheckCredentials.RestCredentials))
            {
                db.HttpClient.DefaultRequestHeaders.Add("X-Version", "2.0");
                var res = await db.GetEventsAsync(ShowInActive).ConfigureAwait(false);
                var obs = new ObservableCollection<EventsDTO>(res);
                return obs;
            }
        }
        catch (Exception ex)
        {
            logger.Error(ex);
            MessageBox.Show(
                $"{ex.Message}{Environment.NewLine}{ex.InnerException?.ToString() ?? ""}");
            return null;
        }
    }

and on the controller

public class EventV2Controller : ApiController
{
    [ApiVersion("2.0")]
    [RoutePrefix("api/events")]

and the config.

  config.AddApiVersioning(cfg =>
        {
            cfg.DefaultApiVersion = new ApiVersion(1, 0);
            cfg.AssumeDefaultVersionWhenUnspecified = true;
            cfg.ReportApiVersions = true;
            cfg.ApiVersionReader = new HeaderApiVersionReader("X-Version");
        });

Upvotes: 0

Related Questions