I am encountering an issue with my code:
Currently, I am utilizing .NET 6 with MVC and integrating FullcalendarIo. I have a controller specifically for creating free slots in the calendar. Below is the snippet of code from the controller:
[Authorize(Roles = DoctorRoleName)]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task < ActionResult < AppointmentSlotInputModel >> GenerateSlots(AppointmentSlotInputModel model) {
//await this.appointmentService.GenerateSlots(model.Start, model.End, model.SlotDurationMinutes);
return Json("Hello");
}
Moreover, here is the JavaScript code responsible for making the POST request:
const params = {
start: startDate,
end: endDate,
slotDurationMinutes: scale
};
const response = await fetch('/Appointment/GenerateSlots', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'RequestVerificationToken': document.getElementById("RequestVerificationToken").value
},
body: JSON.stringify(params)
});
Upon inspecting the network tab, when sending a request to /Appointment/GenerateSlots, I receive a 302 - Redirect followed by a 404 Not Found error. The URL of the request appears correct - https://localhost:44376/Appointment/GenerateSlots.
If I switch the method to GET and include the [HttpGet] attribute above the action in the controller, I successfully obtain the JSON result. In my Startup.cs file, I have configured the following:
services.AddAntiforgery(options => {
options.HeaderName = "X-CSRF-TOKEN";
});
services.Configure < CookiePolicyOptions > (options => {
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
Despite trying various solutions suggested on similar threads, such as one found here - ASP.NET controller, AJAX GET works, but POST does not (404), I have yet to resolve the issue. Any insights on where I may be going wrong?
I am employing standard routing:
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});