ASP.NET Core MVC, Web API, and API Versioning In The Same Project

Saturday, November 25, 2017


Ask Yourself Why Is This Hard

I've had the situation happen to me every now and then where I'm doing something like, let's say, cutting the grass. I lament to my neighbour that cutting the grass takes forever and it hurts my back after a while. My neighbour then points out that while it's possible to cut my grass with a machete, it would probably be much easier with a lawn mower and my back wouldn't hurt anymore. On a side note, I know someone that cuts grass with a machete (in North America), but I won't mention this person, because I don't mess with people who cut their grass with a machete (in North America).

TL;DR

I created an ASP.NET Core MVC app and then added an API controller in the same project for a console application to call. I then added API versioning to my API controller, which then caused versioning to apply to all my controllers. In the end, I realized I didn't need to have versioning for my API controller in my situation. That's probably why I was having difficulty with my situation in the first place and why I couldn't really find any material to solve my problem, despite my best Google-fu efforts. If I ever did need to do this for some reason down the road, I found the answer in a Github issue thread for API versioning. I also included a code snippet below of the way I would do it, which is the recommended way.

Technically Speaking

Having an ASP.NET Core MVC app with an API controller (which is really just another controller) is only a problem when I add API versioning. I can do it, but I didn't realize that API versioning is something that I'm only "supposed" to use when creating a full blown web API (not Web API, no existe in .NET Core). My initial plan was to add API versioning. My thinking was that I didn't want to break things for this one client that was going to call my API and that there would potentially be more clients connecting to this service. There's a couple of things here that I failed to realize, because I was so focused on just getting the versioning to work. I'm going to pass this over to the expert.

Chris Martinez To The Rescue

I think Chris Martinez takes care of the API versioning documentation or at the very least has contributed to it. There's a thread in the issues section for API versioning on Github with someone trying to do something similar to what I'm trying to do. Reading through the whole thing was enlightening. Chris offers solutions to fix the issue, but the best part is that he doesn't just offer a solution, he questions why this is something you would do, because the person that asked the question, like in my case, owns both the client and the server (web API). His question made me question why I would need versioning for only one client that would be connecting to my web API. If there were more than one I would consider versioning and then opt out of my UI controllers the way he recommends, in one place in Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddApiVersioning( cfg => cfg.Conventions.Controller<HomeController>().IsApiVersionNeutral() );
    services.AddApiVersioning( cfg => cfg.Conventions.Controller<AboutController>().IsApiVersionNeutral() );
    ...
}

In the end, I decided not to use API versioning in this case. Only one client will be calling it and I'll know about all future changes for both, so I can plan and test things accordingly, when the time comes. I just had to stop and question why I was versioning my API controller and it no longer made sense. Well, Chris questioned me, even though he wasn't talking me. It was me cutting my lawn with a machete, all over again.


More Posts