我收到一個我似乎無法解決的錯誤。
'沒有給出對應于所需形式引數的引數'
[HttpGet("basket/{identifier}")]
public async Task<OrderDTO> FetchBasket(string identifier)
{
var httpRequestMessage = new HttpRequestMessage(
HttpMethod.Get,
$"https://localhost:5500/api/Basket/{identifier}")
{
Headers = { { HeaderNames.Accept, "application/json" }, }
};
var httpClient = httpClientFactory.CreateClient();
using var httpResponseMessage =
await httpClient.SendAsync(httpRequestMessage);
OrderDTO orderDTO = null;
if (!httpResponseMessage.IsSuccessStatusCode)
return orderDTO;
using var contentStream =
await httpResponseMessage.Content.ReadAsStreamAsync();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var orderServiceDtoExtract = await JsonSerializer.DeserializeAsync
<OrderLine>(contentStream, options);
orderDTO = new OrderLine // I'm getting the error here
{
ProductId = orderServiceDtoExtract.ProductId,
Quantity = orderServiceDtoExtract.Quantity
};
return orderDTO; // 200 OK
}
這是我的模型:
public class OrderLine
{
public OrderLine(int productId, int quantity)
{
ProductId = productId;
Quantity = quantity;
}
public int Id { get; set; }
public int ProductId { get; set; }
public int Quantity { get; set; }
}
uj5u.com熱心網友回復:
創建OrderLine型別的實體時,您使用的是物件初始值設定項語法。但是,因為您顯式地創建了一個帶有非零引數的構造函式,所以您必須先呼叫它。
orderDTO = new OrderLine(orderServiceDtoExtract.ProductId, orderServiceDtoExtract.Quantity);
或者,您可以在呼叫建構式后使用物件初始值設定項語法:
orderDTO = new OrderLine(orderServiceDtoExtract.ProductId, orderServiceDtoExtract.Quantity)
{
// set more properties...
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/403421.html
標籤:
下一篇:如何呼叫泛型方法
