I am working on a simple react + asp.net core application where I want to simply add records to the database (created using entityframework core). There is a very simple form with just a textbox and a button. When clicking the button, I receive the following error:
Error: Request failed with status code 405
I did debugging in runtime, and the Create method in the controller class is not evoked. That is, somehow the axios does not recognize the url provided. Below is the client side code:
class Form extends Component {
state = { rubricName: '' }
handleFormSubmit = (event) => {
event.preventDefault();
axios({
method: 'post',
url: 'api/Rubric/Create',
data: {
title: this.state.rubricName,
},
headers: {
'Content-Type': 'text/plain;charset=utf-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
},
}).then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});
Here is the controller api class:
[Route("api/[controller]")]
public class RubricController : Controller
{
RubricDataAccessLayer objRubric = new RubricDataAccessLayer();
[HttpPost]
[Route("api/Rubric/Create")]
public int Create(string title)
{
return objRubric.AddRubric(new Rubric { Title = title });
}
}
Any ideas about why I am having this error?
OPTIONSrequest that's actually being sent and getting that405? With[Route(...)]commented-out, the endpoint would be justapi/Rubric.[Route()]also set'Access-Control-Allow-Originto*. Still no luck... Any further ideas?Routeattribute the resulting url is followingapi/Rubric/api/Rubric/Create.