Error MVC-controller ASP MVC "404 not found" when decorating with [HttpPost]

I have a controller action method that works when it looks like this:

public ActionResult testMethod(int id) { //do something... //return something... return View(); } 

But when I point out that this should be the Publish method, I get the "404 not found" error:

 [HttpPost] public ActionResult testMethod(int id) { //do something... //return something... return View(); } 

I have other controller action methods in one controller - both POST and GET, and they work fine. But is it not? What's happening? (I'm sure I'm missing something obvious here ...)

Update: Ajax call requests the controller method: var id = 1;

 $.ajax({ url: '/indices/testMethod/', data: id, type: 'POST', success: function (data) { //Do something } }); 

I also tried to test the method using Postman, making sure the POST request was sent.

Update 2: I tried changing the parameter to id and trying to make sure that all areas of the methods and URLs were capitalized to match, but without any effect.

In Fiddler, I see that the GET request is actually being executed, although I specify the POST request in the ajax call, so now I need to find out why the request ends up sending as GET, not POST.

I also tried to include an attribute route description like

  [HttpPost] [Route("indices/TestMethod/{id:int}")] public ActionResult TestMethod(int id) 

And then tried an ajax call with a different url:

 $.ajax({ url: '/indices/TestMethod/1', data: id, type: 'POST', success: function (data) { var tr = 123; var yr = data; //Do something } }); 

When I turn on the attribute and parameter in the URL, I see in Fiddler that the POST request is first executed, which receives a 301 status error, but then the GET request, which receives the 404 error, is also executed.

Update 3: After further research, I narrowed down the problem definition enough to make sense to open a new question, which can be found here: ASP MVC jQuery $ .ajax POST request does not call the controller method, but works in the "fresh" MVC project

The problem seems to be caused by the content security policy settings that were active for this project.

+6
source share
2 answers

Perhaps you need to add the FromBody attribute to the int param1 parameter, since this is a simple type? See here for more details.

Otherwise, a route in the format /indices/testMethod/{param1} is expected (that is, the parameter must be in the URL, even if it is a POST request). This route cannot be determined, i.e. In your global.asax.cs . For example, you can try to make a POST request /indices/testMethod/1 . Just try, is this really a routing problem?

If you are having problems with your routing, you can define a specific route for your method using Attribute Routing . At least for testing / development, as it makes it easier to view / understand which route is actually defined for your method.

Some additional questions to narrow down what it might actually be:

  • What format do you have a dataGoesHere object in?
  • What do similar POST methods look like? What type of data is their parameter? Type of reference or value?
  • What routes do you register (i.e. in global.asax.cs )?

Update

I would try the following:

Define the controller method as follows:

 [HttpPost] [Route("indices/TestMethod/")] public ActionResult TestMethod([FromBody] int id) 

Define the action call as follows:

 $.ajax({ url: '/indices/TestMethod/', data: id, // alternatively try: {id: id} type: 'POST', success: function (data) { } }); 

Try passing id as a simple value or try passing it with an object with id -property (see comment above).

0
source

Try changing param1 to id and see if it works. This may seem silly, but actually I experienced your problem in front of me, and this solved the problem.

Another problem I could think of is that in your application, the testMethod path has /Indices/TestMethod with capital letters. And when you request /indices/testMethod/ , some redirection may occur, which leads to a GET request.

Try changing the url in your ajax call, or try adding the [Route()] attribute to set the route name.

0
source

All Articles