TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.Net Mvc Action Selectors

ASP.Net Mvc Action Selectors with asp.net tutorial, asp.net introduction, features, project, example, server controls, labels, textbox, button, hyperlink, radiobutton, calender, checkbox, fileupload, events handling, authentication, webforms model binding, html server control, compare validdator, range validator, validation summary, mvc introduction, mvc project, view, validation, entity framework, authentication etc.

<< Back to ASP

ASP.NET MVC Action Selectors

Action 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:

  1. ActionName
  2. ActionVerbs

ActionName

This 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.cs

using 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".

ASP Action 1

ActionVerbs

ASP.NET MVC provides action verbs that are applied on the action methods and works for HttpRequest methods. There are various ActionVerbs and listed below.

  • HttpPost
  • HttpGet
  • HttpPut
  • HttpDelete
  • HttpOptions
  • HttpPatch

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.cs

using 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.

ASP Action 2

It produces the error message, when we make a get request for the store action method.

ASP Action 3




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf