简体   繁体   English

混合与否asp.net mvc应用程序和Web API

[英]Mix or not asp.net mvc application and Web API

I have an MVC application. 我有一个MVC应用程序。 I have build two layers BLL and DAL and it is ready to use. 我已经构建了两层BLLDAL ,它已经可以使用了。

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. 我想添加一个服务层:我了解了Asp.Net Web API ,我想用它来构建这个层。

In the book of Adam Freeman ( Pro Asp.net MVC4) I read this 在Adam Freeman(Pro Asp.net MVC4)的书中,我读到了这一点

You can freely mix regular controllers and API controllers in a project. 您可以在项目中自由混合常规控制器和API控制器。 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. 实际上,如果要支持HTML客户端,通常需要这样做,因为API控制器只返回对象数据而不会呈现视图。

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) 使用Asp.Net MVC应用程序 Web API(作为服务层)
  2. Mixing the two applications in a web API application 在Web API应用程序中混合使用这两个应用程序

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控制器和MVC控制器,但WebAPI可以完全独立完成任务。

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). WebAPI控制器不能像您的应用程序那样充当“服务层”,因为它只是将部分功能公开为可以使用HTTP请求(例如来自客户端的AJAX调用)调用的RESTful服务。

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. 应将MVC和的WebAPI控制器中使用你的服务层(业务层),只有暴露你通过的WebAPI需要的数据。 It is not possible to use the WebAPI methods in place of your business layer. 无法使用WebAPI方法代替业务层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM