简体   繁体   English

如何从不同项目中的MVC控制器调用WebAPI?

[英]How to call WebAPI from a MVC Controller in a different project?

I have to create a Web API for my existing MVC project and make API Controllers calling the Service Layer and Models, which is contained in separate projects but in the same Solution, then create and map to DTOs. 我必须为我现有的MVC项目创建一个Web API,并使API控制器调用服务层和模型(包含在单独的项目中,但包含在同一解决方案中),然后创建并映射到DTO。

Layout of my Projects Solution in VS ( ignore the BookService.cs . I was just trying to do a WebAPI tutorial and I put it in the same solution temporarily). 我在VS中的项目解决方案的布局 (忽略BookService.cs 。我只是在尝试编写WebAPI教程,然后将它暂时放在了相同的解决方案中)。

I have been reading up on WebAPI and how it functions for the past 2 days, but I am not being able to fully grasp an understanding on how to create a API Controllers for my MVC project without referencing it? 在过去的两天里,我一直在阅读WebAPI及其功能,但是我无法完全理解如何为我的MVC项目创建API控制器? I also have to make Views at the end, in my main project calling the uri, but I am very confused at this point. 我还必须在名为uri的主项目中最后制作Views,但是在这一点上,我感到非常困惑。

It would help me out a lot if someone can please clarify how I am to tackle this or point me to a tutorial or some sort of source to learn the process of working with Web API. 如果有人可以阐明我该如何解决这个问题,或者为我提供教程或某种来源来学习使用Web API的过程,那么这将对我有很大帮助。 Thank you. 谢谢。

The Web Api project will be a separate "website", that you will need to host individually. Web Api项目将是一个单独的“网站”,您需要单独进行托管。 Your MVC project will make requests to the Web Api using HttpClient . 您的MVC项目将使用HttpClient向Web Api发出请求。

Since the Web Api will be separate, you won't be able to utilize helpers like Url.RouteUrl , etc. to get URLs for the Web Api actions. 由于Web Api是独立的,因此您将无法利用Url.RouteUrl等帮助Url.RouteUrl来获取Web Api操作的URL。 You will also just need to know the full URI to the Web Api, including it's domain. 您还只需要知道 Web Api的完整URI,包括它的域。 There will be no way to programmatically ascertain this information, so I would recommend making use of Application Settings to avoid hardcoding in your MVC project. 无法以编程方式确定此信息,因此,我建议使用“应用程序设置”以避免在MVC项目中进行硬编码。

Right-click on your MVC project in the Solution Explorer and choose Properties. 在解决方案资源管理器中右键单击您的MVC项目,然后选择“属性”。 Then click over to the Settings tab. 然后单击“设置”选项卡。 Here, you can add strongly-typed settings that your MVC application can utilize. 在这里,您可以添加MVC应用程序可以利用的强类型设置。 Importantly, these settings are still persisted in the Web.config, so you can change them using config transforms. 重要的是,这些设置仍保留在Web.config中,因此您可以使用config转换来更改它们。 Your Web Api will likely have different URLs depending on whether you're in development vs. production, for example, so that will make it very easy to ensure that you're hitting the right thing in the right environment. 例如,根据您是在开发还是生产中,您的Web Api可能会有不同的URL,因此可以轻松确保在正确的环境中找到正确的事物。

You can add a setting like WebApiUri , and give it a type of System.Uri . 您可以添加类似WebApiUri的设置,并为其指定System.Uri类型。 Then, set it to the string value of where your Web Api is hosted in development , ie http://localhost:12345 . 然后,将其设置为在开发中托管Web Api的字符串值,即http://localhost:12345 It's important that the setting be specific to your development environment, as config transforms are not applied in development. 设置必须特定于您的开发环境非常重要,因为配置转换不会在开发中应用。 For staging, production, etc. you'll change this setting appropriately in the applicable config transform, and it will be updated to the right value for the right environment when you publish. 对于登台,生产等,您将在适用的配置转换中适当地更改此设置,并且在发布时会针对正确的环境将其更新为正确的值。

Then, when you need to work with it, you'll just do something like: 然后,当您需要使用它时,您将只需执行以下操作:

var client = new HttpClient();
client.BaseAddress = Properties.Settings.Default.WebApiUri;

Then, just make requests as normal through the client. 然后,只需通过客户端发出正常请求即可。 For more information on working with HttpClient , see the documentation . 有关使用HttpClient更多信息,请参见文档

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

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