Scenario
I'm using Syncfusion UI controls in a Razor Pages Net6 application, but I'm having issues with antiforgery validation when requesting data for the grid...
Example:
// ------------- Books.cshtml
@page
@model Site.Pages.BooksModel
@using Syncfusion.EJ2
@{
}
@Html.EJS().Grid("BooksIndexGrid").DataSource(dataManager => {
dataManager.Url("/books?handler=GetGridData");
dataManager.Adaptor("UrlAdaptor");
}).Columns(col =>
{
col.Field("title").Add();
}).AllowPaging().AllowSorting().Render()
//------------- Books.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Site.Pages
{
public class BooksModel : PageModel
{
private readonly IMediator _mediator;
public BooksModel(IMediator mediator)
{
_mediator = mediator;
}
public void OnGet()
{
}
public async Task<JsonResult> OnPostGetGridDataAsync(DataManagerRequest dm)
{
//Filtering logic removed for brevity
var data = await _mediator.Send(new GetListOfBooksRequest());
return new JsonResult(new { result = data, count = data.Count() });
}
}
}
Issue
The example above returns a 400 error code because it requests the data using a POST method and the Antiforgery token is not present in the request. So the question is...
How do I add the required Antiforgery token to the request to read the data?
Ive found this article on creating a custom Adapter, but it only seems to cover CUD and not CRUD:
I know I can disable the check for the antiforgery token, but do not wish to do that.