Web API Namespaces vs MVC Namespaces

tl;dr: You should never have references to System.Web.Mvc.* in an ApiController.

One thing that took me longer than I’d care to admit to realize when working with Web API and ApiController is that Web API has its own namespace to do a lot of the stuff I was used to System.Web.Mvc doing.

So, for example, there’s a System.Web.Mvc.HttpPostAttribute class that you’d use to decorate a controller action to tell it to accept HTTP POST requests.

If you’re trying to do that on an ApiController action, you’ll want to use System.Web.Http.HttpPostAttribute.

The code ends up looking the same, mostly, though, because instead of including using System.Web.Mvc; at the top, you’ll have using System.Web.Http, and you’ll end up writing the attribute as just [HttpPost].

This doesn’t just apply to those attributes, either. Web API uses that System.Web.Http stuff for action filters, configuration, and plenty of other stuff. You should never need to include System.Web.Mvc.* for anything in an API controller.