Grid Read Handler Method not found. 404 error

1 Answer 317 Views
Grid
Vakho
Top achievements
Rank 1
Vakho asked on 31 Oct 2022, 05:58 AM | edited on 31 Oct 2022, 06:01 AM

Hi, I have a grid on my razor page but the browser is giving me a 404 error saying that the specified method (/Tournaments/Organizer/Details?handler=ReadTournamentTeams) could not be found.

My "DataSource" portion of grid (.cshtml)

.DataSource(ds => ds.Ajax()
	   .Read(r => r.Url("/Tournaments/Organizer/Details?handler=ReadTournamentTeams"))
	   .Model(m =>
	   {
		   m.Id(id => id.TeamID);
	   })
	.PageSize(10)
)

 

My method in .cshtml.cs:

public JsonResult OnPostReadTournamentTeams([DataSourceRequest] DataSourceRequest request)
{
	var tournamentTeams = db.Tournament_X_Teams.Where(x => x.Active == true).ToList();
	return new JsonResult(tournamentTeams.ToDataSourceResult(request));
}

 

What can the issue be?

Kendo Version: 2022.2.621

Visual Studio: VS 2022, v17.3.6

1 Answer, 1 is accepted

Sort by
0
Stoyan
Telerik team
answered on 02 Nov 2022, 03:33 PM

Hi Vakho,

Thank you for sharing the configuration of the DataSource you use.

I am assuming that the server-side has a route that can respond to a call to a call to /Tournaments/Organizer/Details. 

This leads me to suspect that the Grid sends a GET request and the issue occurs because the ReadTournamentTeams will only respond to a POST.

To test this could you please inspect the Network tab of your browser's DevTools. There you can inspect the header of the request and see whether that is true.

If it is, you can configure the Read request to be send as a GET:

.Read(r => r.Url("/Tournaments/Organizer/Details?handler=ReadTournamentTeams").Type(HttpVerbs.Get))

I hope the suggestion above is helpful in resolving the issue. 

Regards,
Stoyan
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Vakho
Top achievements
Rank 1
commented on 04 Nov 2022, 02:58 AM

Hi Stoyan, I inspected the network tab and the request is actually POST. I also tried changing my method name to "OnGetReadTournamentTeams" but still same issue occurs.
Stoyan
Telerik team
commented on 04 Nov 2022, 11:36 AM

Hi Vakho,

Could you share an isolated sample project with dummy data that reproduces the reported issue? This will enable me to look through the structure of the routes and research the issue in more depth.

Thank you for your consideration in advance.

Vakho
Top achievements
Rank 1
commented on 11 Nov 2022, 02:42 AM

Hi Stoyan, I was able to figure out my problem (partially). You can see what it was here (see my comments at the bottom):

https://www.telerik.com/forums/grid-with-asp-net-core-razor-page---404-on-index-handler=read

If you have any additional input/advice, or have an answer to the last question I asked on that thread, I'd appreciate that.

Thanks.

Stoyan
Telerik team
commented on 14 Nov 2022, 08:17 PM

Hi Vakho,

I am happy the initial issue has been resolved and thank you for sharing a link to the thread that showcases the solution.

Indeed Razor Pages use AntiForgery Tokens to by default to protect from XSRF/CSRF and requests to the server are rejected, if the token is missing. To resolve this you need to add the AntiForgery Tokens to the CRUD request of the Grid:

.Read(r => r.Url("/Index?handler=Read").Data("forgeryToken"))
.Update(u => u.Url("/Index?handler=Update").Data("forgeryToken"))
.Create(c => c.Url("/Index?handler=Create").Data("forgeryToken"))
.Destroy(d => d.Url("/Index?handler=Destroy").Data("forgeryToken"))
In addition you are able to add more data by extending the object return by the handler of the Data configuration property:
    function forgeryToken() {
        var token = kendo.antiForgeryTokens();
        return { data: "my_data", __RequestVerificationToken: token.__RequestVerificationToken };
    }

Vakho
Top achievements
Rank 1
commented on 16 Nov 2022, 05:55 AM

Hi Stoyan, that worked! thanks.

But I have seen other telerik projects where ONLY custom data was returned to methods. and it worked just fine.
For instance, a grid with this line:

.Read(r => r.Url("/Index?handler=Read").Data("myCustomMethodReturningOnlyCustomData"))

Did you guys change something in recent version of kendo where forgery tokens are required? or is it possible that those projects were somehow configured to ignore whether requests contain forgery tokens or not?

 

 

Stoyan
Telerik team
commented on 16 Nov 2022, 10:20 AM

Hi Vakho, 

The requirement of the server for AntiForgery Token validation is a security feature that comes built-in with Razor Pages. It is unrelated to Telerik UI.

You can disable that feature in the Page's Model:

namespace MyProject.Pages
{
    [IgnoreAntiforgeryToken]
    public class CreateOrderModel : PageModel
    {
...

or at the sevice level
services.AddAntiforgery(options => { options.SuppressXFrameOptionsHeader = true; });

For more information on the topic refer to the related StackOverflow thread.

MR
Top achievements
Rank 1
commented on 30 Jan 2025, 01:59 AM | edited

Adding attribute on PageModel [IgnoreAntiforgeryToken] still gives 404.

How to send Token as well as working? Can you please post full HTML View and Code behind code for Combo Box A and B?

Tags
Grid
Asked by
Vakho
Top achievements
Rank 1
Answers by
Stoyan
Telerik team
Share this question
or