简体   繁体   中英

Asp.Net core Web API calling

I am new in Asp.net Core and trying to clear my concept on web api. I have basic knowledge on 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.

My problem is, I add an web api in a web application project named "Api_BusinessUnit". How can I call this web api in a controller named "BusinessUnitController" shown in below image. 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" ?

在此输入图像描述

Thanks in advance.

Why do you want to have one Web API call another in the same project? 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.

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. 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

For me, I am a Scott Allen fan and he explains the project structure for the particular structure you are trying to create here.
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.

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. Then you're off to the races.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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