From the course: Advanced ASP.NET Web API 2.2

Creating the first versioned controller

From the course: Advanced ASP.NET Web API 2.2

Start my 1-month free trial

Creating the first versioned controller

- [Instructor] To be able to use any of the versioning methods, we need to first setup the environment by creating our first version controllers. So, let us say that we are in a case where the response data is changing. We want to add more details on what we want to return to the users. So, let us go to Visual Studio. Since we want to change the response data, we need to start by changing the model. Let us go to the model and create another one by copy and pasting existing one. So let us name the Student to StudentV2 and change the class name to StudentV2. Let us also change Student.cs to StudentV1. Let's modify the StudentV2 so it returns more data. So in here, let us write prop int Age. And now let's go inside our data folder. In here we have the IStudentRepository, so let's add in here one more method. And let's that method GetAllV2 and let us change the first one to GetAllV1. Now let's go to our controller, and change the name of this controller from StudentsController to StudentsV1Controller and let's create another one named StudentsV2Controller. Now let us start with the StudentsV1Controller. So we see here that the name has changed and also replace in line 28, replace the GetAll with GetAllV1. Let's go now to the StudentsV2. Change the name in here to StudentsV2Controller and the constructors name StudentsV2 and change the method in here to GetAllV2. So we see that we have the StudentsV2Controller and the StudentsV1Controller. Now, let us go to the StudentRepository.cs file and in here, since now the IStudentRepository is changed, We need to change these methods in here, the implementation of this repository. So, let us save this one to GetAllV1. And let us create one more, named GetAllV2. One last thing in here, let us change the GetStudentsFromDb to GetStudentsFromDbV1 and let us copy the line 26 to 46 by pressing Ctrl + C. Paste it in here, change that to GetStudentsFromDbV2. It will return an IEnumberable of StudentV2. So in here, change this to V2 and List in here. Now we need to change these two values. And let us add in here, the value age. So age is equal to 22. And another age in here is equal to 24. Don't forget the comma in here. Let us just change the full names in here from Jimmy to just Jimmy Version2, just so we know the difference. The same way for the John Smith. Now, save the changes and scroll up to the GetAllStudents. So, in here from the GetAllV1, write GetStudentsFromDbV1. And for the other one, V2. And make sure that you change the response type in here to StudentV2. Let us remove this one, since we are not going to use but to not have any compile time errors just go to the interface and remove it from here, as well. Now, let us save all the changes and build our solution. Okay, so let's go inside here. And then let's see what we are missing in here. Yeah, so let's go back to the interface and in here, even though we the method is GetAllV2, we are returning still the version one students, so let's change that to V2 and save the changes. So we see that the error is gone. And this is how you can create your first version controllers.

Contents