Actions
Action methods are the methods which have one to one mapping with user requests. These methods execute requests and create aa response. Each Controller can have default action method, however that needs to be declared in RegisteRoutes() method of RouteConfig file.
Action Selectors
Action selectors are the attributes which can be applied to action methods. With the help of action selectors, routing engine selects the correct action method for handling specific request. Action selectors also decide the behaviour of method on the basis of name given in front of the action method.
There are three types of action selectors attributes,
- ActionName
- NonAction
- ActionVerbs
ActionName
ActionName attribute allows you to use different name than the actual name of method.
Below is the sample for the same.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace FirstAspNetMVC.Controllers {
- public class EmplyeeController: Controller {
-
- public string Index() {
- return "This is ASP.Net MVC ActionName";
- }
- [ActionName("GetDetail")]
- public string Detail() {
- return "This is detail at " + DateTime.Now.ToString("T");
- }
- }
- }
Now, in the above example, ActionName attribute is used as GetDetail; hence after running the application, you need to enter the following URL in the browser http://localhost:62833/Emplyee/ GetDetail
NonAction
NonAction is another built-in attribute which indicates that a method of a Controller is not an action method. It is used when you want a method not to be treated as an action method.
Below is the sample for the same.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace FirstAspNetMVC.Controllers {
- public class EmplyeeController: Controller {
-
- public string Index() {
- return "This is ASP.Net MVC ActionName";
- }
- [ActionName("GetDetail")]
- public string Detail() {
- return "This is detail at " + GetTimeAsString;
- }
- [NonAction]
- public string GetTimeAsString() {
- return "This is detail at " + DateTime.Now.ToString("T");
- }
- }
- }
Now, in the above example, GetTimeAsstring() will be treated as non-action and it will not be available for accessing through URL.
ActionVerbs
You can restrict indication of a specific action to a specific HttpVerb. Now, with the help of action verbs, you can define two different methods of the same, however each one will have different action verb like one can have Http Get another can hove Http Post.
ASP.NET MVC framework supports the following ActionVerbs.
- HttpGet - To fetch information from Server.
- HttpPost- To post information on Server.
- HttpPut - To updated existing information on Server.
- HttpDelete- To delete existing information from Server.
- HttpOptions- It represents a request for information about the communication options supported by web Server.
- HttpPatch- For full or partial update of the resource.
Below is the sample for HttpGet and HttpPost.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using EmployeeMgrMvc.Models;
- namespace EmployeeMgrMvc.Controllers {
- public class EmployeeController: Controller {
- public ActionResult Index() {
- northwndEntities nw = new northwndEntities();
- return View(nw.Employees);
- }
- public ActionResult Edit(int id) {
- return View(GetEmployee(id));
- }
- [HttpPost]
- public ActionResult Edit(Employee emp) {
- try {
- northwndEntities nw = new northwndEntities();
- Employee origEmp = GetEmployee(emp.EmployeeID);
- nw.Employees.Attach(emp);
- nw.ApplyOriginalValues("Employees", origEmp);
- nw.SaveChanges();
- return RedirectToAction("Index");
- } catch {
- return View();
- }
- }
- }
In the above example, we have created Edit action methods, one for HttpGet and another one for HttpPost.
Note
By default, ASP.NET MVC framework considers HttpGet on action methods.