Currently, I am working on a project using Angular8 and .NET Core 3.0 in Visual Studio. Everything is running smoothly except for one issue that arises when I press F5 on a page with a form. The error message that pops up reads:
Failed to load resource: the server responded with a status of 405 (Method Not Allowed) [http://localhost:51871/notifications]
The submit function in the component class looks like this:
onSubmit() {
if (this.registerForm.invalid) {
return;
}
let post: NotificationsInterface = {
email: this.registerForm.value.email,
subject: this.registerForm.value.subject,
text: this.registerForm.value.text,
};
this.http.post("Notifications", post).subscribe(result => {
console.error("ok");
}, error => console.error("error"));
}
This is the C# notification class:
public class Notifications
{
public string email { get; set; }
public string subject { get; set; }
public string text { get; set; }
}
And here is the controller in C#:
using Microsoft.AspNetCore.Mvc;
using HomeHelper.Models;
namespace HomeHelper.Controllers
{
[ApiController]
[Route("[controller]")]
public class NotificationsController : Controller
{
[HttpPost]
public IActionResult Post([FromBody]Notifications notification)
{
return Ok();
}
}
}
I have noticed that by commenting out the controller part in C#, the error disappears but data cannot be received. What would be the best approach to prevent such errors in the future? This particular error only occurs in the URL "http://localhost:51871/notifications".
UPDATE: I have added the full code of the controller along with the specified error-inducing URL.