简体   繁体   English

将WebAPI添加到现有的MVC5应用程序

[英]Adding WebAPI to an existing MVC5 application

I have an MVC5 application, which works normally. 我有一个MVC5应用程序,可以正常工作。 It has models, controllers, views, and all the other functionality. 它具有模型,控制器,视图和所有其他功能。 It is already a stand-alone app. 它已经是一个独立的应用程序。 I now want to create an API for this application. 我现在想为此应用程序创建一个API。 I don't know whether I should create a new WebAPI project in the same solution, or use my existing MVC project, to also include the API functionality there. 我不知道该在同一个解决方案中创建一个新的WebAPI项目,还是使用现有的MVC项目在其中包含API功能。 Which is the appropriate way? 哪种方法合适? Please note that later I want to use that API for creating a mobile application using Xamarin. 请注意,稍后我想使用该API来使用Xamarin创建移动应用程序。

There is nothing wrong with using the same project, in many ways it makes the most sense as it will be easier when hosting the application if they live within the same web project. 使用同一项目没有任何问题,在许多方面它都是最有意义的,因为如果应用程序驻留在同一Web项目中,则在托管应用程序时会更容易。

I tend to keep my API controllers in a subfolder called "Api" which is neatly located within the Controllers folder of the web solution. 我倾向于将API控制器保存在一个名为“ Api”的子文件夹中,该子文件夹整齐地位于Web解决方案的Controllers文件夹中。

"Controllers > Api". “控制器> Api”。

When i'm not sure with these kinds of things I tend to go with the simplest option, later on down the line if you find a good reason to locate the controllers in their own project for some reason this is easily achieved. 当我不确定这些事情时,我倾向于采用最简单的选择,如果您出于某种原因很容易实现将控制器放置在自己项目中的理由,请稍后再进行讨论。

You mentioned that you will be using the API to develop an application using XAMARIN later on and this should not mean moving the API... mobile apps will not need any kind of reference to the API at all, they will simply be calling your application using standard HTTP requests. 您提到过,稍后您将使用API​​使用XAMARIN开发应用程序,但这并不意味着要移动API ...移动应用程序根本不需要任何对该API的引用,它们只是在调用您的应用程序使用标准的HTTP请求。 It will make no difference and again as you are going to need a host for your website, you might as well use the same host for your API too. 并没有什么不同,因为您将需要网站的主机,所以您最好也为API使用相同的主机。

如果您不仅要对WebSite使用WebAPI可以肯定的是,您已经在解决方案中添加了新的WebAPI项目(+在OOP更好)-更改很容易。

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

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