image by Amanda
I've heard a lot of developers saying that WebAPI is a valid replacement for ASP.NET MVC. I'm here to argue for ASP.NET MVC, even though I agree with the detractors that it is beginning to show its age.
I assume when people say that WebAPI is a replacement, they also mean to mention that Single Page Applications (SPA) are the new way to build web applications, but as you can tell, I am not completely sold on that method of development (yet).
I will attempt to point out four things that ASP.NET MVC has that make it a better choice for web application development than a WebAPI application.
The view engine infrastructure in ASP.NET MVC is amazing. It is something that ASP.NET MVC developers take for granted. All I've seen from WebAPI developers are half attempts at making view engines that aren't as robust as what ASP.NET MVC has to offer. If you are going to render HTML on the server, you need a view engine.
When you have view engines, you have to deal with views. What you'll need is a way to create HTML elements and partial views onto the page. HTML Helpers are easy to write and extend in ASP.NET MVC, while they are non-existent in WebAPI.
The ability to execute child actions is a huge plus for ASP.NET MVC. I get separation of concerns while allowing me to compose pages from different actions. I get all this sent to the client in one single request. To accomplish the same thing in a WebAPI call, I'd have to make multiple requests or merge concerns.
ASP.NET MVC has a large open source ecosystem, and while WebAPI is catching up, it is nowhere near the depth and quality that ASP.NET MVC has.
I don't want you to think I am a complete ASP.NET MVC fanboy and that I turn a blind eye to some of the failing of the framework. Like I mentioned in the opening of this post, ASP.NET MVC has started showing its warts. I believe it needs a breaking rewrite to stay relevant in today's technology stack. A Few of these things include testing, async, and the ability to detach from IIS.
Testing has always been annoying to do with ASP.NET MVC, and WebAPI offers a much nicer experience here. The difficulty in ASP.NET MVC testing usually comes from the pain of mocking objects like HttpContextBase.
You might say to yourself, "ASP.NET MVC already has async" and you'd be partially correct. ASP.NET MVC currently supports async at the action level, but is not async anywhere else in the pipeline (by default). This can cause chaos if your controllers are async but your filters are not. To achieve async correctly, you must modify the ASP.NET MVC pipeline yourself, which can get a little scary and cause deadlock issues that paralyze your server when done incorrectly.
Detach From IIS
WebAPI can be hosted anywhere there is a valid OWIN host. ASP.NET MVC currently does not have this option since it is tightly coupled to IIS. OWIN might change this, but there is still a lot of work to do.
WebAPI was developed to be a framework for creating HTTP APIs, but it lacks a lot of the features required of a complete web framework. That is why it annoys me when others say WebAPI is the replacement for ASP.NET MVC. If you want to argue for a viable replacement for ASP.NET MVC, I would argue that Nancy is the best contender out there to replace both ASP.NET MVC and WebAPI. It has view engines, html helpers, routing,OWIN support, great testing story, and async support.