Why does mvc attribute routing in an HttpGet action also affect HttpPost without a routing attribute, is this an error?

I have two actions: one for HttpGet with this signature:

[Route("NewsLetter/SelectEmail/{page?}")] [HttpGet] public ActionResult SelectEmail(int? page, string priCat, string secCat) { ... } 

And one for HttpPost with this signature:

 [HttpPost] [ValidateAntiForgeryToken] public ActionResult SelectEmail(int id) { ... } 

After setting the above route for the HttpGet method, I noticed that the other method with HttpPost stops working, after I dig, I realized that the route for HttpGet also set for HttpPost >, and this did not work until I explicitly set for its routing attribute:

 [Route("NewsLetter/SelectEmail/{id}")] [HttpPost] [ValidateAntiForgeryToken] public ActionResult SelectEmail(int id) { ... } 

I wanted to know is this a mistake? If this is not the case, do you still need to set the routing attribute for [HttpGet] without also executing the corresponding [HttpPost] ?

+5
source share
1 answer

You cannot use POST and GET at the same time, because your action will accept requests with any HTTP methods. Try using AcceptVerbsAttribute to restrict the HTTP methods in your RouteTable. https://msdn.microsoft.com/en-us/library/system.web.mvc.acceptverbsattribute(v=vs.118).aspx

+1
source

All Articles