简体   繁体   English

.NET Core解决方案中同时具有API和MVC项目的约定

[英]Conventions on having both an API and MVC project in .NET Core solution

I have an ASP.NET Core (.NET Core 2.2) app structured with the following projects: 我有一个包含以下项目的ASP.NET Core(.NET Core 2.2)应用程序:

  • API: this is meant to represent a WebAPI (with controllers inheriting ControllerBase ) API:旨在表示一个WebAPI(控制器继承了ControllerBase
  • Services: This contains services which the API controllers utilize to access the database, etc 服务:包含API控制器用来访问数据库等的服务
  • Database: This contains the usual DB repositories, which the services layer utilize to access the database 数据库:它包含常规的数据库存储库,服务层利用这些数据库存储库来访问数据库

Now, I want to add a UI that talks to the API (the MVC part pre-.NET-core). 现在,我想添加一个与API对话的UI(MVC部分位于.NET核心之前)。 How is that accomplished with .NET Core, where MVC and WebAPI are one of the same thing? 如何通过.NET Core(在其中MVC和WebAPI是同一件事)来实现这一目标? Should MVC controllers/models/views be part of the API? MVC控制器/模型/视图是否应包含在API中? Should it instead be a new project that listens on a different port? 它应该是一个在其他端口上侦听的新项目吗? How does authentication fit in for both (eg APIs usually have some token-based authentication, UI apps usually have username/password authentication)? 身份验证如何同时适合两者(例如,API通常具有基于令牌的身份验证,UI应用程序通常具有用户名/密码身份验证)? Should the WebAPI and MVC portions share the same authentication like ASP.NET Identity? WebAPI和MVC部分是否应该像ASP.NET Identity一样共享相同的身份验证? Wouldn't that tightly couple the two if they use the same database? 如果两者使用相同的数据库,那会不会紧密地结合在一起?

Is there some kind of Microsoft or community suggested convention/documentation for how to structure such projects? 是否有某种Microsoft或社区建议的约定/文档来说明如何构建此类项目?

I think that you are a bit confused about WebAPI compared to MVC. 与MVC相比,我认为您对WebAPI有点困惑。

You can see WebAPI as simple web services answering http request with data (whatever the data is, it could even include javascript or assets). 您可以将WebAPI看作是简单的Web服务,它用数据回答http请求(无论数据是什么,它甚至可以包括javascript或资产)。

EDIT: So sending "UI" informations is definetly a part of your API and Service project. 编辑:因此,发送“ UI”信息无疑是您的API和Service项目的一部分。

On API you will need to create dedicated controller(s) to send back your "UI" part(s). 在API上,您将需要创建专用的控制器以发送回“ UI”部分。 On Service you will need to create dedicated service(s) to fetch the "UI" informations (their is many way to do this, using Ressources, fetching data on Cloud, etc) 在服务上,您将需要创建专用服务以获取“ UI”信息(使用资源,在Cloud上获取数据等多种方式)。

EDIT2: But nothing prevent you from creating an entirely different solution for UI parts. EDIT2:但是没有什么可以阻止您为UI部件创建完全不同的解决方案。 If you chose WebAPI again, you will still need to enforce the previously mentioned API/Service logic. 如果再次选择WebAPI,则仍然需要强制执行前面提到的API /服务逻辑。 It's up to you to chose whatever you feel confortable with. 您可以选择自己喜欢的方式。

How is that accomplished with .NET Core, where MVC and WebAPI are one of the same thing? 如何通过.NET Core(在其中MVC和WebAPI是同一件事)来实现这一目标?

In dotnet core MVC and WebAPI can be present in the same project. 在dotnet核心中,MVC和WebAPI可以存在于同一项目中。 Everything application is like a console application. 一切应用程序都像控制台应用程序。 You can add MVC services to startup class to make it an MVC application. 您可以将MVC服务添加到启动类以使其成为MVC应用程序。

Should MVC controllers/models/views be part of the API? MVC控制器/模型/视图是否应包含在API中?

Its better to have different controllers for MVC and WebAPI related functions separately while keeping them in the same folder. 最好分别具有与MVC和WebAPI相关功能的不同控制器,同时将它们保留在同一文件夹中。

Models - They can be reused for both mvc and webapi. 模型-它们可同时用于mvc和webapi。 Same for view models and DTOs. 视图模型和DTO相同。

Views - Just for MVC, webapi does not require views. 视图-仅对于MVC,webapi不需要视图。

Should it instead be a new project that listens on a different port? 它应该是一个在其他端口上侦听的新项目吗?

Yes, you can create a different project for webapi and MVC. 是的,您可以为webapi和MVC创建一个不同的项目。

How does authentication fit in for both (eg APIs usually have some token-based authentication, UI apps usually have username/password authentication)? 身份验证如何同时适合两者(例如,API通常具有基于令牌的身份验证,UI应用程序通常具有用户名/密码身份验证)?

If you use token-based authentication then both web API and MVC will be able to use. 如果您使用基于令牌的身份验证,那么Web API和MVC都将可以使用。

Should the WebAPI and MVC portions share the same authentication like ASP.NET Identity? WebAPI和MVC部分是否应该像ASP.NET Identity一样共享相同的身份验证? Wouldn't that tightly couple the two if they use the same database? 如果两者使用相同的数据库,那会不会紧密地结合在一起?

If you use ASP.Net Identity with identity server then both MVC and webapi will be able to share the same authentication mechanism without tightly coupling. 如果将ASP.Net Identity与身份服务器一起使用,则MVC和webapi都将能够共享相同的身份验证机制,而无需紧密耦合。

The answer to your question is mostly, "it depends on your tastes" but in my opinion... 您的问题的答案主要是“取决于您的口味”,但我认为...

Unless you are planning on exposing the API to other applications, keep the API controllers in the same application that hosts the MVC controllers (or Razor Page). 除非您打算将API公开给其他应用程序,否则将API控制器保留在承载MVC控制器(或Razor Page)的同一应用程序中。 When I have both MVC controllers and API controllers I put them under separate folders. 当我同时拥有MVC控制器和API控制器时,我将它们放在单独的文件夹中。 I think this is OK, because your controllers should be very thin. 我认为可以,因为您的控制器应该很薄。 I generally put all the business logic (including any necessary data access) in services that are built in a separate class library. 我通常将所有业务逻辑(包括任何必要的数据访问权限)放在单独的类库中构建的服务中。

You only add an API if you actually need it. 仅在实际需要时添加API。

Do you plan to expose anything to another app? 您打算将任何内容公开给另一个应用程序吗?

If all you want is a UI which interacts with a database then don't bother, use the services to retrieve the data, called them from the MVC controllers and skip the API part completely. 如果您只需要一个与数据库交互的UI,那么就不用打扰了,使用服务检索数据,从MVC控制器中调用它们,然后完全跳过API部分。

You don't need an API for such a limited use case. 对于这种有限的用例,您不需要API。 An API introduces a host of other things to consider, like authentication and security. API引入了许多其他要考虑的因素,例如身份验证和安全性。 Don't complicate things when you don't need to. 不需要时不要使事情复杂化。

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

相关问题 面向.net核心api约定的重构 - refactoring towards.net core api conventions .net核心解决方案 - 项目结构 - .net Core Solution - Project stucture 使用集成的Web Api启动.NET MVC项目时遇到问题 - Having trouble starting .NET MVC project with Web Api integrated 如何在 api mvc 项目 net core 3.1 中向 controller 添加操作 - How to add action to controller in api mvc project net core 3.1 MVC5项目-命名约定 - MVC5 Project - Naming Conventions ASP.NET MVC 5 Web API不接受来自MVC ASP.NET Core解决方案的POST上的视图模型 - ASP.NET MVC 5 Web API not accepting View Model on POST from MVC ASP.NET Core solution MVC解决方案中的Web API在单独的项目中 - Web API in MVC solution in separate project 带有mvc和Web api项目的msbuild在单个解决方案中 - msbuild with mvc and web api project in a single solution Net Core API 依赖注入,无需在 API 项目中引用存储库库 - Net Core API Dependency injection without having repository library to be referenced in API project 如果可能,您可以从其解决方案/项目文件夹中托管一个.Net核心Web API并通过IIS进行调试 - If possible can you host a .Net core web API from it's solution/project folder and debug though IIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM