0:00 / 0:00 The ApiVersionReader has two options, query string ApiVersionReader and header ApiVersionReader, as well as URL segment ApiVersionReader.

Currently, we have enabled URL segment ApiVersionReader, and we have other two options.

That means, the users can include the version number of the API either as a route parameter, or as a query string parameter, or as a request header.

Currently, we have enabled URL segment, which means that the api-version number can be a route parameter like this.

Let us try to enable query string ApiVersionReader.

So, instead of this, we are writing an alternative statement, that is, ApiVersionReader equal to query string ApiVersionReader.

So, this is an alternative statement for URL segment ApiVersionReader, which means that the version number cannot be included in the route parameter like this.

So, the route template look like this normally, API slash controller, but whereas it is the responsibility of the user to mention the api-version number as a part of the query string parameter.

And the query string parameter is expected as API slash version.

You can also change the same explicitly if you want.

For example, you can mention the same as just version, for example, like this.

Now, the query string that is expected is just version instead of API slash version.

Okay, let it be the default.

So, the query string should be API slash version for the time being.

Now, run the application and test it.

Now, if you make a request to API slash cities, now question mark, api-version equal to the particular version that you want.

For example, 1.0.

So, this is the actual URL.

That is, question mark, API hyphen version equal to 1.0.

Okay.

Now, it executes the version one of cities controller.

In the same way, you can send request to api-version 2.0.

And it works accordingly.

But here the question is, what if the user did not mention anything in the api-version?

For example, he just omits the same.

In this case, we are getting error code.

An api-version is required, but it was not specified.

So, in order to handle this error, you can mention a default api-version.

Something like, if the user doesn’t mention any particular api-version, you can make 1.0 as default.

It can be either 1.0 or 2.0, whatever you want, based on your requirement.

In order to specify the default version, you can write like,

a configuration, specify the default version number as,

a new api-version, that is 1,0, major version number and minor.

Now onwards, it understands that, okay, this 1.0 is the default version number.

But that is not enough.

We have to write one more, that is, assume default version when unspecified equal to true.

So, the property name is self-explanatory.

If the user doesn’t mention any particular version number,

assume the default version number, that is 1.0, as mentioned above.

So, run this application and test it right now.

Let’s mention the URL as, api slash cities without version number, like this.

Just it is api slash cities.

I did not mention the api-version query string parameter,

but it automatically assumes the version number as 1.0 in this case,

because it is the default version number as per the code.

As a user, if I would like to request second version,

then we have to write explicitly api-version equal to true, like this.

Okay.

So, this is the second option for ApiVersionReader.

Now, the last and third option is ApiVersionReader equal to,

that is, header ApiVersionReader.

That means, now it expects a request header that must be present,

that is, api-version equal to the version number value,

for example, 1.0 or 2.0, whatever it wants.

And you may need to mention the request header name explicitly here,

that is, for example, api slash version.

It can be anything, but you should mention the same request header

while making a request.

But you cannot add a request header from the browser directly,

but you need Postman in order to add a custom request header.

So, the request URL is api slash cities,

but go to the headers, I mean request headers,

add a key, that is, api-version,

name it correctly, api-version,

and the value can be either 1.0 or 2.0,

or just integer value 1 or 2 also.

It by default takes 0.0.

For example, I am just writing 1.

So, it executes the version 1,

so it gives the data in the form of JSON as expected.

But if you mention api-version equal to 2.0 or just 2,

now it executes the second version of the cities controller

and gives the response from the same.

In this case, the first version doesn’t execute.

So, these are the three options for version reader,

but whereas the first one itself is mostly used in the real-world applications,

because it adds flexibility for the end users,

they can easily request the particular version number,

whatever they want, as a part of the route parameter.

So, let’s re-enable the same,

that is, UrlSegmentApiVersionReader for subsequent lectures.

But still, the unsolved problem is

when you run this application through Swagger,

we are getting this type of strange error,

saying that response status is 500,

and if you see the network requests,

and when you make a request to swagger.json at this URL,

we are getting error saying that conflicting the method path

api slash cities at get request

is having conflict between version 1 cities controller

and version 2 cities controller,

because the Swagger still don’t know

how do you manage the versions of your endpoints.

So, we request to write some more configuration settings

for Swagger to work.

Let’s do that in the next lecture.

Play Play Play Play Stop Play Start Start Start