Reputation: 1015
In MVC 5, there aren't GET and POST attributes. There are Route attributes. It looks like these will work with any request. Is there a way to specify that I only want to allow gets to a certain method with attribute routing? I tried using HttpGet but I got the following error: 'System.Web.Mvc.HttpPostAttribute' does not contain a constructor that takes 1 arguments
I'm going to try tagging [HttpGet] over my route attribute and test that out (should look like this):
[HttpGet]
[Route("GetUsers")]
public ActionResult GetUsers()
{
//return view with users
}
Upvotes: 1
Views: 1004
Reputation: 139768
The ASP.NET MVC routing does not care about the verb of the request so in order to restrict specific actions to use specific HTTP verbs in MVC5 with attribute routing, you still need to use the existing HTTP verbs attributes (e.g. [System.Web.Mvc.HttpGetAttribute]
, [System.Web.Mvc.HttpPostAttribute]
and [System.Web.Mvc.AcceptVerbsAttribute]
)
So as in your example:
[HttpGet]
[Route("GetUsers")]
public ActionResult GetUsers()
{
//return view with users
}
Or using the C# multiple attribute syntax:
[Route("GetUsers"), HttpGet]
public ActionResult GetUsers()
{
//return view with users
}
Upvotes: 3