This workshop will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Web API has been merged into MVC. Let's look at an example of an API controller to understand the impact of this change.
Additional Learning
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
The big change of API controllers
from the previous versions of MVC and
0:00
Web API is that they are no
longer separate products.
0:04
Web API has been merged into MVC.
0:08
This means that MVC and API controllers
now share the same base class.
0:12
This convergence makes it much easier
to share your code between your MVC and
0:18
API controllers.
0:23
For instance,
a single custom filter can now be used for
0:25
both MVC and API controllers.
0:28
Even though it's possible for
0:32
a controller to contain action methods
that return view content or API content.
0:33
I still like to separate my API action
methods into their own controllers.
0:39
I think this makes your
projects easier to navigate
0:44
by making your code more discoverable.
0:47
Here's an example of
a simple API controller.
0:50
Notice that we can use the controller
token in the route attribute template.
0:53
The controller token will get updated
with our controller class name minus
0:59
the controller suffix.
1:04
This makes it easier to keep the class
name in sync with the route template.
1:05
If we were ever to change
the name of our controller class.
1:09
Our action method names no
longer implicitly influence or
1:15
affect the list of allowed HTTP verbs.
1:19
So we now need to be explicit
with the verbs that our action
1:23
methods should respond to by using
the appropriate HTTP verb attributes.
1:26
The last thing to note here is that
we can now provide the route template
1:32
directly in the HTTP verb attribute.
1:36
This is a nice improvement to
help make our code more concise.
1:39
Let's start our app and
use Postman to test our API.
1:43
Here's the response from our API.
2:02
Notice that our property
names are camelcased.
2:05
Instead of starting with a capital letter
like our actual model property names.
2:07
This is the new default behavior for
ASP.NET core.
2:12
If you're developing a new app, you
just need to remember to use the correct
2:16
property names in your client side code.
2:20
If you're migrating an app from
the previous version of Web API,
2:23
this change might break
your client side code.
2:26
In our Startup class,
we can configure the MVC JSON options.
2:41
So that our model property names are not
changed when rendering a JSON response.
2:45
This code is getting
the JSON DefaultContractResolver and
2:54
then removing the camelcasing
naming strategy.
2:58
If we start our app again and
make another GET request.
3:02
We can now see that our model
property names are being preserved.
3:05
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up