Search code examples
asp.net-mvcasp.net-web-apimef

web api vs asp.net mvc api


I want to write a app that needs to have 2 things: 1. REST API so others can retrieve data and display their own ways 2. The default display is a web page using ASP.NET MVC

So there are some approaches: 1. Create 2 separate projects, 1 for Web API and another is ASP.NET MVC. The MVC will talk to Web API to get the data & render Views 2. Create a single ASP.NET MVC project. And create a class that derives from APIController. Then do the similar thing as above.

As I understand, the performance should be the same for both approaches. But after I read this article , I'm not quite sure.

Quote from article:

"You could do that with an ASP.NET MVC “API” by creating a separate project for the API, but then you’re still paying the performance penalty for your API requests to filter through MVC’s rendering pipeline. In simple load testing on my local machine, I’ve found that Web API endpoints hosted in console apps are nearly 50% faster than both ASP.NET controller actions and Web API endpoints hosted within MVC projects."

I'm no expert in ASP.NET so I'd love to hear your explanation or advise how to achieve what I want to do.

Also, please enlighten me if I want to use MEF for this project.

Thanks very much.


Solution

  • I think it is not related to ASP.NET MVC, but to the selected hosting. If you are finding a console host 50% much faster is because it does not have all the overhead of ASP.NET. The self-host for ASP.NET Web API mounts on top of the WCF Http Listener (which internally uses http.sys) so the communication is more direct, no ASP.NET involved at all (no even IIS). I don't think MVC has much impact on that. If the Web API and the pages are pretty much related, I would keep them in the same project.