To mix or not use asp.net mvc app and web api

I have an MVC application. I have created two layers BLL and DAL and it is ready to use.

I would like to add a service layer: I learned about Asp.Net Web API

and I would like to use it to create this layer.

In Adam Freeman's book (Pro Asp.net MVC4) I read this

You can freely mix common controllers and API controllers in your project. In fact, you usually need it if you want to support HTML clients, because the API controllers will only return the object data and won't render the view.

So, I would like to know in which cases I should use one of these solutions:

  • Using Asp.Net MVC Application and Web API (as Service Layer)
  • Mixing two apps in a web app app
+3


source to share


1 answer


Yes, it is good to use WebAPI controllers and MVC controllers in the same project or in separate projects, but WebAPI does a completely separate function.

WebAPI controllers cannot act as a "service layer" per se for your application, as it simply exposes pieces of functionality as a RESTful service that can be invoked using HTTP requests (such as AJAX calls from the client).



You should use your service layer (business layer) in both your MVC and WebAPI controllers, and only expose the data you need through the WebAPI. There is no way to use WebAPI methods in place of your business layer.

+5


source







All Articles