Serialize DataSource Request of Grid and Post It to Another Backend Service
Environment
Product | Telerik UI for ASP.NET Core Grid |
Product Version | 2019.3.917 |
ASP.NET Core Version | 3.0 |
Description
I've started using SpreadStreamProcessing and I'm now trying to get it to work with my existing code. I would like to pass the DataSourceRequest
to another method and use it to generate an excel file. How can I post the existing request?
Solution
- For get requests, you can use an approach similar to the one demonstrated in this forum post.
- For the standard POST request, you need to serialize the Form request in the same way as the grid request does it.
public ActionResult ReadOrders([DataSourceRequest]DataSourceRequest request)
{
var req = JsonConvert.SerializeObject(HttpContext.Request.Form);
ProcessRequest(req);
return Json(orders.ToDataSourceResult(request));
}
private async void ProcessRequest(string request)
{
HttpClient httpClient = new HttpClient();
var formItems = new List<KeyValuePair<string,string>>();
httpClient.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
var values = JsonConvert.DeserializeObject<List<KeyValuePair<string,List<string>>>>(request);
foreach (var keyValuePair in values)
{
formItems.Add(new KeyValuePair<string,string>(keyValuePair.Key), keyValuePair.Value[0]));
}
var form = new FormUrlEncodedContent(formItems);
var response = httpClient.PostAsync("https://SomeOtherDomain.com/SomeController/ReadOrders", form).Result;
string resWithFilter = await response.Content.ReadAsStringAsync();
List<OrderViewModel> savedResult = JsonConvert.DeserializeObject<ResultData>(resWithFilter).Data;
}