Home > DeveloperSection > Forums > How can we restrict MVC actions to be invoked only by GET or POST?
Anupam Mishra

Total Post:135

Points:949
Posted on    February-14-2016 10:48 PM

 ASP.Net C#  ASP.Net  ASP.NET MVC 
Ratings:


 1 Reply(s)
 578  View(s)
Rate this:
Hi All,
How can we restrict MVC actions to be invoked only by GET or POST? Please give me a solution.

thanx.


Anupam Mishra

Total Post:135

Points:949
Posted on    February-14-2016 11:01 PM

I have got it, its answer. Details explanation is below:
We can restrict the MVC action with the HttpGet or HttpPost attribute to restrict the type of HTTP calls. For example,In below code DisplayStudent action can only be invoked by HttpGet. If we try to make HTTP POST on DisplayStudent, it will throw an error.


[HttpGet]
public ViewResult DisplayStudent(int id)
{
    Student objStudent = Studentss[id];
    return View("DisplayStudent",objStudent);

Modified On Feb-14-2016 11:02:28 PM

Don't want to miss updates? Please click the below button!

Follow MindStick