Http PostAsJsonAsync 获取空参数(DTO)

Http PostAsJsonAsync getting null parameter (DTO)

我正在尝试将 DTO class 对象传递到我的服务器,但该对象正在发送 null

DTO:

public class FileSettings
{
    public int SetID { get; set; }
    public int RunID { get; set; }
    public int FileID { get; set; }
}

客户:

string URL = "http://localhost:9000";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);

FileSettings data = new FileSettings();
data.SetID = setID;
data.RunID = runID;
data.FileID = fileID;

HttpContent content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.PostAsJsonAsync("/api/output/ApplyFileRule", content);

服务器:

public void ApplyFileRule([FromBody] FileSettings data)
{
    var runID = data.RunID;
    ...
}

更改这些:

FileSettings data = new FileSettings();
FileSettings.SetID = setID;
FileSettings.RunID = runID;
FileSettings.FileID = fileID;

HttpContent content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");

为此:

FileSettings data = new FileSettings();
data.SetID = setID;
data.RunID = runID;
data.FileID = fileID;

HttpContent content = new StringContent(data, Encoding.UTF8, "application/json");