如何用request对象调用.Net内核中的端点

yhuiod9q  于 2023-03-04  发布在  .NET
关注(0)|答案(2)|浏览(99)

我在API 1中有一个如下所示的端点

[HttpPost]
public ActionResult PostSchoolQuery([FromBody] SchoolQueryModel schoolQueryModel, [FromHeader] string authorization)
{

}

这里的请求模型类如下所示

public class SchoolQueryModel
{
 public List<Guid?> SchoolIds { get; set; }
 public List<Guid?> DistrictIds { get; set; }
}

当我尝试从API 2调用端点PostSchoolQuery时,如下所示,在api-1中,我总是收到空值

public ActionResult GetUserSchools(SchoolQueryModel getSchoolsModel)
        {
           dynamic schoolDetails = null;          
            var requestContent = new JsonSerializer.Serialize(getSchoolsModel);
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Authorization", _infrastructureAuthKey);
                var responseTask = client.PostAsync("http://localhost:6200/api/post_school_query", requestContent);
                if (responseTask.Result.IsSuccessStatusCode)
                {
                    var readTask = responseTask.Result.Content.ReadAsAsync<JObject>();
                    readTask.Wait();
                    schoolDetails = readTask.Result;
                }
            }
  }

请告诉我一声,谢谢

2izufjch

2izufjch1#

试试这个PostAsJsonAsync

public ActionResult GetUserSchools(SchoolQueryModel getSchoolsModel)
{
    dynamic schoolDetails = null;

    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Authorization", _infrastructureAuthKey);
        var responseTask = client.PostAsJsonAsync("http://localhost:6200/api/post_school_query", getSchoolsModel);
        if (responseTask.Result.IsSuccessStatusCode)
        {
            var readTask = responseTask.Result.Content.ReadAsAsync<JObject>();
            readTask.Wait();
            schoolDetails = readTask.Result;
        }
    }
}
cgfeq70w

cgfeq70w2#

这一条对我很有效,我还介绍了一些更好的做法:

using System.Text;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;

namespace AspNetPlayground.Controllers;

[ApiController]
[Route("[controller]/[action]")]
public class TestController : ControllerBase
{
    private readonly HttpClient _httpClient;

    public TestController(IHttpClientFactory httpClientFactory) => _httpClient = httpClientFactory.CreateClient();

    [HttpPost]
    public Task<bool> IsNotNull([FromBody] SchoolQueryModel? schoolQueryModel) => Task.FromResult(schoolQueryModel is not null);

    [HttpPost]
    public async Task<bool> Run()
    {
        var getSchoolsModel = new SchoolQueryModel {SchoolIds = new() {Guid.NewGuid()}, DistrictIds = new() {Guid.NewGuid()}};
        using var requestContent = new StringContent(JsonSerializer.Serialize(getSchoolsModel), Encoding.UTF8, "application/json");
        using var response = await _httpClient.PostAsync("https://localhost:7041/Test/IsNotNull", requestContent);
        return response.IsSuccessStatusCode && await response.Content.ReadFromJsonAsync<bool>();
    }
}

public class SchoolQueryModel
{
    public List<Guid?> SchoolIds { get; init; } = new();
    public List<Guid?> DistrictIds { get; init; } = new();
}

相关问题