Action selectors are defined as the attributes that are applied to controller action methods to influence the selection of an action method. So, if we are writing all the logic in the controller as a single Action Method then that will become a complex for us to maintain it.
The ActionName attribute is used when we expose an action name with a different name than its method name, or we can use an action name attribute to expose the two different methods with the same name as the action with different names. This ActionName selector attribute is used to change the name of the action method. Following given example shows how to change the name of action method using ActionName attribute
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Tutorial4.Controllers
{
public class HomeController : Controller
{
[ActionName("Home")]
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View("Index");
}
}
}
In the above code, we can see that we changed the name of ActionMethod from Index to Home using ActionName selector. Now we can call this Action method in the following way: http://localhost:1111/home/home
[HttpGet], [HttpPost], [HttpPut], [HttpDelete] Attributes in Action Selectors
[HttpGet] – used to restrict an action method to handle only HTTP GET requests.
[HttpPost] - used to restrict an action method to handle only HTTP POST requests.
[HttpPut] -used to restrict an action method to handle only HTTP PUT requests.
[HttpDelete] - used to restrict an action method to handle only HTTP DELETE requests.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Action selectors are defined as the attributes that are applied to controller action methods to influence the selection of an action method. So, if we are writing all the logic in the controller as a single Action Method then that will become a complex for us to maintain it.
Now to solve this problem we need to use Action selectors attributes ([HttpGet], [HttpPost], [HttpPut], [HttpDelete] ) and decorate it with these attributes based on our requirements then we would know which Action Method will execute on which Http Request and also it is easy to maintain.
1. [ActionName("Home")]
2. [HttpGet] , [HttpPost] , [HttpPut] , [HttpDelete]
3. [AcceptVerbs(HttpVerbs.Get)] , [AcceptVerbs(HttpVerbs.Delete)]
4. [AcceptVerbs(HttpVerbs.Post)] ,[AcceptVerbs(HttpVerbs.Put)]
ActionName in Asp.Net MVC
The ActionName attribute is used when we expose an action name with a different name than its method name, or we can use an action name attribute to expose the two different methods with the same name as the action with different names. This ActionName selector attribute is used to change the name of the action method. Following given example shows how to change the name of action method using ActionName attribute
In the above code, we can see that we changed the name of ActionMethod from Index to Home using ActionName selector. Now we can call this Action method in the following way: http://localhost:1111/home/home
[HttpGet], [HttpPost], [HttpPut], [HttpDelete] Attributes in Action Selectors