简体   繁体   English

Asp.Net核心Web API调用

[英]Asp.Net core Web API calling

I am new in Asp.net Core and trying to clear my concept on web api. 我是Asp.net Core的新手,并尝试在web api上清除我的概念。 I have basic knowledge on web api. 我对web api有基本的了解。 I can do CRUD operation using web api by running that web api project and calling it in other web application project at a time. 我可以通过运行web api项目并在其他Web应用程序项目中一次调用它来使用web api进行CRUD操作。

My problem is, I add an web api in a web application project named "Api_BusinessUnit". 我的问题是,我在一个名为“Api_BusinessUnit”的Web应用程序项目中添加了一个web api。 How can I call this web api in a controller named "BusinessUnitController" shown in below image. 如何在名为“BusinessUnitController”的控制器中调用此Web api,如下图所示。 My confusion is, Both are in a same project, I can run one project at a time. 我的困惑是,两者都在同一个项目中,我可以一次运行一个项目。 So how can I use this web api in "BusinessUnitController" ? 那么如何在“BusinessUnitController”中使用这个web api?

在此输入图像描述

Thanks in advance. 提前致谢。

Why do you want to have one Web API call another in the same project? 为什么你想在同一个项目中有另一个Web API调用? If you need to communicate between parts of your project, you can do so directly without having to over the web, which will be much better from a performance perspective and will be more reliable as well. 如果您需要在项目的各个部分之间进行通信,则可以直接进行,而无需通过Web,从性能角度来看,这将更好,并且也将更加可靠。

That said, looking at your image, I think you have two separate web projects in the same solution which isn't the same thing at all (you may wish to update your question if this is the case). 也就是说,看着你的形象,我认为你在同一个解决方案中有两个独立的网络项目,这根本不是一回事(如果是这种情况,你可能希望更新你的问题)。 To have one project communicate with the other project, you should determine the URL of the destination project and call it as you are doing. 要让一个项目与另一个项目进行通信,您应该确定目标项目的URL并按照您的方式调用它。 You also will need to ensure both projects are running, of course. 当然,您还需要确保两个项目都在运行。 You can launch multiple projects at once when you hit F5/ctrl+F5 as shown here: http://ardalis.com/how-to-start-multiple-projects-in-visual-studio 点击F5 / ctrl + F5后,您可以一次启动多个项目,如下所示: http//ardalis.com/how-to-start-multiple-projects-in-visual-studio

For me, I am a Scott Allen fan and he explains the project structure for the particular structure you are trying to create here. 对我来说,我是Scott Allen的粉丝,他解释了你想在这里创建的特定结构的项目结构。
https://odetocode.com/blogs/scott/archive/2013/07/01/on-the-coexistence-of-asp-net-mvc-and-webapi.aspx https://odetocode.com/blogs/scott/archive/2013/07/01/on-the-coexistence-of-asp-net-mvc-and-webapi.aspx

However, I am a fan of best practices and SoC, so the proper way, IMHO, is to have one solution with multiple projects and either keep the entire solution in version control, or have the project solutions separate, build and deploy to a directory will all of them for testing. 但是,我是最佳实践和SoC的粉丝,所以正确的方法,恕我直言,就是拥有一个包含多个项目的解决方案,并将整个解决方案保持在版本控制中,或者将项目解决方案分开,构建并部署到目录将他们全部用于测试。

Then the key factor to running MVC and WebApi as different projects in the same solution, besides making sure that Microsoft.AspNet.WebApi.Core is installed, is that the start up project is the MVC and the WebApi references that MVC. 然后,除了确保安装了Microsoft.AspNet.WebApi.Core之外,在同一解决方案中运行MVC和WebApi作为不同项目的关键因素是启动项目是MVC,而WebApi引用了MVC。 Then you're off to the races. 那你就去参加比赛了。

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

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