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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With