[英]Sending a List or Array to Web Api
我正在創建一個api方法來從文本文件中添加結果。.我的應用程序從文本文件中讀取數據,並填充C#結構調用ResultLines的通用列表。 測試應用程序時,檢查請求時將結果行添加為boby參數並添加到請求中,我正在使用Restsharp來計算我的api。但是當我在api上設置斷點時,我只能看到已經接收到23行,但是值是通過擴展我標記為粗體的模型,始終為null
這是我使用Api的代碼
public HeatResultResponse AddHeatWithList(List<ResultFileLine> rec,string HeatNum, string WebApiUrl)
{
RestClient client = new RestClient();
client.BaseUrl = new Uri(WebApiUrl);
var request = new RestRequest("Controller/InsertNewHeatList", Method.POST);
request.AddHeader("Accept", "application/form-urlencoded");
//client.AddHandler("application/json", new JsonDeserializer());
request.AddHeader("Content-Type", "application/json");
request.RequestFormat = DataFormat.Json;
request.AddParameter("HeatNumber", HeatNum);
rec.ForEach(r =>
request.AddParameter("ResultLines", r)
);
IRestResponse<HeatResultResponse> response2 = client.Execute<HeatResultResponse>(request);
return response2.Data;
}
我的Api方法看起來像這樣
public GetHeatIdResponse InsertNewHeatList(**HeatResultInsertListRequest model**)
{
GetHeatIdResponse resp = new GetHeatIdResponse
{
IsSuccess = false,
Message = "",
HeatId = 0
};
if (model == null)
{
resp.Message = "Request body is empty. Should contain JSON data";
}
else
{
try
{
if (ModelState.IsValid)
{
Heat heat = new Heat();
foreach(ResultFileLine model1 in model.ResultLines)
{
.... Here there is code to populate result to the Db
}
}
}
這是我的模特
public class HeatResultInsertListRequest : BaseRequest
{
[DataMember]
public List<ResultFileLine> ResultLines { get; set; }
public string HeatNumber { get; set; }
}
默認情況下,Web Api僅允許4mb請求數據。
IIS默認情況下將接受26 mb。
因此iis將接受請求,但您的應用將僅接受4mb數據。 這就是為什么請求對象為null的原因
您可以通過在Web.config的HttpRuntime中修改MaxRequestLength來增加它
<httpRuntime maxRequestLength="50000"></httpRuntime>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.