Blazor项目使用相对URI遇连接被主动拒绝错误的排查与解决
Blazor调用相对URI接口失败,绝对URI正常的问题排查与解决
我正在自学Blazor,遇到一个接口调用问题:使用相对URI api/employee 请求时,报错 {"No connection could be made because the target machine actively refused it."},但改用完整URI https://localhost:44376/api/employee 就能正常请求。我希望能使用不带端口号的相对URI发起请求,以下是发起请求的GetAllEmployees方法代码:
public async Task<IEnumerable<Employee>> GetAllEmployees() { bool isEmployeeListEmpty = true;; IEnumerable<Employee> employeeList = null; try { //string uriEmployeeList = $"https://localhost:44376/api/employee"; string uriEmployeeList = $"api/employee"; var employees = await JsonSerializer.DeserializeAsync<IEnumerable<Employee>> (await _httpClient.GetStreamAsync(uriEmployeeList), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }); if (employees != null) { isEmployeeListEmpty = false; employeeList = await JsonSerializer.DeserializeAsync<IEnumerable<Employee>> (await _httpClient.GetStreamAsync(uriEmployeeList), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }); } else { isEmployeeListEmpty = true; } } catch (Exception ex) { Console.WriteLine("An exception occurred inside the GetAllEmployees() method of the EmployeeDataService class"); Console.WriteLine(ex.Message); } if (!isEmployeeListEmpty) return employeeList; else return null; }
开发环境为单机器上的IIS Express。
更新
问题已解决,根源在于:launchSettings.json 中配置的SSL端口为 44376,但Startup.cs里三个HttpClient对象的基地址设置成了https://localhost:44340/。将基地址的端口修改为44376,与launchSettings.json保持一致后,使用相对URI即可正常工作。
内容的提问来源于stack exchange,提问作者Ivan
相关产品推荐
相关产品推荐

