Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route Constraint Error on POST

When you manipulate data in an action, you often receive an ID as param, but you need to do some error handling for that id. One of the error handling you have to do for every action is to make sure the ID is higher than 0 (not a negative number). So instead of handling this in the action, I wanted to add a route constraint so just doesnt route to the action if its a negative id.

Here's my code:

//route definition
        routes.MapRoute(
            "default route" ,
            "{controller}/{action}/{id}" ,
            new { id = UrlParameter.Optional },
            new { id = @"^\d+$" }
        );

//action definition (note I also tried with only [HttpPost] and with nothing same result
        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get )]
        public ActionResult Edit( int id )

It all works fine when you do a GET on the action, but when I POST I get the following error when it should just go to the 404 page

 HTTP verb POST used to access path '/object/edit/-2' is not allowed.

[HttpException (0x80004005): The HTTP verb POST used to access path '/object/edit/-2' is not allowed.]
   System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state) +740
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +632
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +194

Any ideas? Perhaps a better solution?

EDIT: Just noticed something interesting, I initially thought that the error message was a 500, but its a 405 which is "method not found"

like image 573
Pierluc SS Avatar asked Nov 05 '22 01:11

Pierluc SS


1 Answers

You say:

HTTP verb POST used to access path '/object/edit/-2' is not allowed.

However the exception states:

The HTTP verb POST used to access path '/profile/editlink/-2' is not allowed.

Edit:

I think this might be related to your problem.

However it isn't as you state.

like image 61
Ropstah Avatar answered Nov 09 '22 15:11

Ropstah