C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ASP.NET MVC Action SelectorsAction selectors are attributes that are applied on action methods of a controller. It is used to select correct action method to call as per the request. MVC provides the following action selector attributes:
ActionNameThis attribute allows us to specify a different name for the action method. It is useful when we want to call action by different name. Example Here, we are using ActionName attribute to apply different name for the index action method. The controller code looks like this: // MusicStoreController.csusing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplicationDemo.Controllers { public class MusicStoreController : Controller { [ActionName("store")] public ActionResult Index() { return View(); } } } Now, we need to create a view in the MusicStore folder as same as the ActionName. So, we have created a store.cshtml file that has following code. // store.cshtml@{ ViewBag.Title = "store"; } <h2>Hello, This is Music store.</h2> Output: The following output is produced when action is called with different name "store". ActionVerbsASP.NET MVC provides action verbs that are applied on the action methods and works for HttpRequest methods. There are various ActionVerbs and listed below.
ActionVerbs are name of the http requests that a controller handle. We can use it for selection among the action methods. Example In the following example, we are trying to access an index action by get request, which is accessible only for httpPost request. The controller code looks like this: // MusicStoreController.csusing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplicationDemo.Controllers { public class MusicStoreController : Controller { [HttpGet] public ActionResult Index() { return View(); } [HttpPost] public ActionResult Welcome() { return View(); } } } Following is the Index file for MusicStoreController. // index.cshtml<div class="jumbotron"> <h2>Welcome to the music store.</h2> </div> Output: It produces the following output, when the index action is called. It produces the error message, when we make a get request for the store action method.
Next TopicASP.NET MVC Action Filters
|