4

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

I'd like to add a service layer: I learned about Asp.Net Web API and I'd like to use it to build this layer.

In the book of Adam Freeman ( Pro Asp.net MVC4) I read this

You can freely mix regular controllers and API controllers in a project. In fact, you will usually need to if you want to support HTML clients because API controllers will only return object data and will not render a view.

So, I'd like to know in which cases I have to use one of this solution :

  1. Using Asp.Net MVC application and a web API ( as a service layer)
  2. Mixing the two applications in a web API application
7
  • 2
    Yes it is fine to use WebAPI controllers and normal controllers, I tend to use the WebAPI ones for AJAX calls from the client. I would say that a "service layer" is something additional that both your WebAPI controllers and your regular controllers will use in order to gain access to your domain objects. I'm pretty sure that your regular controllers should never call upon your WebAPI controllers... Commented May 28, 2015 at 9:51
  • @Coulton If I understood, you are with the first alternative( separate service layer(web api)) Commented May 28, 2015 at 10:14
  • 1
    The first option isn't technically correct because you wouldn't use WebAPI as a "service layer". Both your WebAPI controllers and regular controllers would be used in a completely different way and both could make use of a generic service layer (generic classes/methods) in order to get the data that they need to perform their duties. If you mix the two in the same project, you aren't really mixing them, IE using one from another, they act independently of one another. You would never call your WebAPI methods from your regular controllers - in fact, I'm not sure it's even possible. Commented May 28, 2015 at 10:19
  • Thanks @Coulton but why not make an asp.net mvc application and web api: In the first's views we make ajax calls to WebAPI controllers. by this way, the structure of the solution will be clearer and more maintenable. Besides, if I mix the two applications, did I keep the interoperability concept ie If i add a php project, can I access to the api controller's methods (services) from it??? Commented May 28, 2015 at 10:38
  • 1
    Great to hear that you already have a BL and DAL. Yes that sounds exactly like what you should do. Just create WebAPI controllers and methods for getting the data that you need for your views and call them through AJAX requests. Good luck :) Commented May 28, 2015 at 11:00

1 Answer 1

5

Yes it is fine to use WebAPI controllers and MVC controllers in the same project, or in separate projects, but WebAPI serves a completely separate duty.

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

You should use your service layer (business layer) within both your MVC and WebAPI controllers and only expose the data that you need through WebAPI. It is not possible to use the WebAPI methods in place of your business layer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.