ASP.NET MVC Core - 通过 HttpClient 使用 API

系统

我有一个包含两个项目的 Visual Studio 解决方案;API 和 MVC。MVC 是包含我的视图等的 MVC 项目,API 是包含 API 端点和逻辑的 API 项目。由于我有两个项目,我已将我的解决方案配置为具有多个启动项目。

客户端

public const string AuctionAPI = "http://localhost:44337/";
public const string AuctionClient = "http://localhost:44398/";

public static HttpClient GetClient()
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(AuctionAPI);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    return client;
}

MVC 控制器(注意:我在第 6 行收到错误,当我设置 HttpResponseMessage 并尝试调用 client.GetAsync()

[Route("item/{itemNumber}")]
public async Task<IActionResult> IndexForItem(int itemNumber)
{
    var client = GetClient();
    //ERROR: In next line, HttpResponseMessage it tries to call my API controller and fails
    HttpResponseMessage response = await client.GetAsync($@"api/Auction/SeeOneItem/{itemNumber}");

    if (response.IsSuccessStatusCode)
    {
        string content = await response.Content.ReadAsStringAsync();
        var model = await response.Content.ReadAsAsync<AuctionItemProxy>();

        return View(model);
    }
    else
    {
        return Content("An error occured.");
    }
}

API 控制器(注意:我在此方法中的断点从未命中)

[HttpGet("SeeOneItem/{itemNumber:int}")]
public AuctionItem GetAuctionItem(int itemNumber)
{
    var item = _auctionItemDataFactory.AuctionItems.First(x => x.ItemNumber == itemNumber);
    if (item == null)
    {
        return null;
    }
    return _auctionItemDataFactory.AuctionItems.First(x => x.ItemNumber == itemNumber);
}

我收到的实际错误

IOException: Unable to read data from the transport connection

塔里克·图通库

此错误意味着目标机器可访问,但您尝试访问的服务不可访问。

通过查看您共享的代码,我注意到一件事。如果项目设置为使用 Https,则 IIS Express 会分配以 443 开头的端口,从而产生类似 443xx 的端口。将您的 http 方案更改为 https 并重试。

public const string AuctionAPI = "http://localhost:44337/";

public const string AuctionAPI = "https://localhost:44337/";

或者

将您的项目设置更改为不使用 SSL

  1. 右键单击您的项目转到属性
  2. 在属性窗口中转到调试部分
  3. 在 Web 服务器设置下取消选中“启用 SSL”

此问题也可能与传输级安全有关。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过使用AddMvcCore()实现“纯” ASP.NET Core Web API

ASP.NET Core MVC组通过LINQ查询

在ASP.Net Core MVC中使用AJAX提交表单

使用DotNetZip通过ASP.NET MVC下载zip文件

如何在ASP.NET Core应用中使用HttpClient

使用ASP.NET Core和MVC存储本地文件

使用没有MVC的ASP.NET Core

ASP.Net MVC:如何通过ASP.NET Core中的中间件重写URL

在ASP.NET MVC Core Web API和Entity Framework Core中使用TryUpdateModel更新模型不起作用

何时在ASP.NET Core MVC中使用ResultFilter

如何在ASP.NET Core 2.0 API中使用HttpClient

使用Identity Core的ASP.NET MVC框架中的身份

如何从ASP.NET Core MVC中的HttpClient类调用[HttpPatch]类型的API动作

测试使用ASP.NET Core API通过Startup.ConfigureServices()配置的Polly重试polly

如何使用单独的Core API保护ASP.NET Core MVC客户端?

如何使用Entity Framework通过ASP.NET Core Web API处理一般查询?

如何从Web API调用ASP.NET Core Web MVC

通过Jquery使用ASP.NET MVC验证

Asp.Net MVC Core 2 - 在 _Layout 上使用 ViewBag

将 ASP.NET Core Web API 与不使用 MVC 的应用程序一起使用

ASP.NET Core 通过 Postman 调用 API

HttpClient 是我在 .NET Core MVC 中使用 API 的最佳选择吗

使用 asp.net core 3.1 mvc 的级联下拉列表

如何从 .NET Core api 中的 MVC 视图使用 API?

使用依赖注入 ASP.Net Core MVC 编辑操作

如何在 ASP.NET Core MVC 中通过“int”使用搜索功能?

在 ASP.NET Core 的外部库中使用 HttpClient

ASP.NET Core MVC 調用 API

ASP.NET Core Web API - 如何使用 HttpClient 使用 3rd 方 API