Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why mvc attribute routing on HttpGet action also effect HttpPost without a routing attribute, is it a bug?

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 aforementioned route for HttpGet method, I've noticed that the other method with HttpPost has stopped working, after digging around I've realized that the route for HttpGet also set itself for HttpPost, and it didn't work until I explicitly set a routing attribute for it:

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

I wanted to know, is it a bug?, if it's not, is there anyway to set a routing attribute for a [HttpGet] without also effecting the corresponding [HttpPost]?

like image 442
Hamid Mosalla Avatar asked Nov 10 '22 05:11

Hamid Mosalla


1 Answers

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

like image 183
joint_ops Avatar answered Nov 15 '22 06:11

joint_ops