what is Action Name and Action Selectors in Asp.Net MVC with Example
Action Selectors, Action Name in Asp.Net MVC with Example:
Action selectors are attribute which are applied to Controller Action Methods to influence the selection of an action method. If we are writing all logic in Controller as a single Action Method that will become complex to maintain it.
To solve this problem we need to use Action selectors attribute ([HttpGet] , [HttpPost] , [HttpPut] , [HttpDelete] ) and decorate it with this attribute then we would know which Action Method will executed on which Http Request and Easy to Maintain.
- [ActionName("Home")]
- [HttpGet] , [HttpPost] , [HttpPut] , [HttpDelete]
- [AcceptVerbs(HttpVerbs.Get)] , [AcceptVerbs(HttpVerbs.Delete)]
- [AcceptVerbs(HttpVerbs.Post)] ,[AcceptVerbs(HttpVerbs.Put)]
Action Name
This Action Name attribute is used when you expose an action name with a different name than its method name, or you can use an action name attribute to expose two methods with the same name as the action with different names. The ActionName selector attributes are use to change the name of action method. Following example shows how we can change 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");
}
}
}
Here in above code you can see that we changed name of ActionMethod from Index to Home using ActionName selector. Now you can call this Action method in following way: http://localhost:1111/home/home
Attributes in ActionSelectors
[HttpGet] - Represents an attribute that is used to restrict an action method so that the method handles only HTTP GET requests.
[HttpPost] - Represents an attribute that is used to restrict an action method so that the method handles only HTTP POST requests.
[HttpPut] - Represents an attribute that is used to restrict an action method so that the method handles only HTTP PUT requests.
[HttpDelete] - Represents an attribute that is used to restrict an action method so that the method handles only HTTP DELETE requests.
MVC framework routing engine uses Action Selectors attributes to determine which action method to invoke.
Three action selectors attributes are available in MVC 5
- ActionName
- NonAction
- ActionVerbs
ActionName attribute is used to specify different name of action than method name.
NonAction attribute marks the public method of controller class as non-action method. It cannot be invoked.
MVC framework routing engine uses Action Selectors attributes to determine which action method to invoke.
Three action selectors attributes are available in MVC 5
- ActionName
- NonAction
- ActionVerbs
ActionName attribute is used to specify different name of action than method name.
NonAction attribute marks the public method of controller class as non-action method. It cannot be invoked.
Comments
Post a Comment