Action Selector are the attributes that are applied to the action methods .Action selector helps in the routing engine to select particular action method to handle a particular user request. In this article I will explain two of them action name and Non action .Action verb in the next article.
1-Action Name
2-Non Action
3-Action Verb
Action Name
Action Name is the attribute that represent the action name. let us take an example to understand action name.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DemoStoreproc.Models;
using System.Data;
using System.Data.Entity;
namespace DemoStoreproc.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
var context = new DemoEntities();
ViewBag.Country =context.CountryLists;
ViewBag.users = context.RegistrationForms.ToList();
return View();
}
public string Gettime()
{
return DateTime.Now.ToString();
}
}
}
Now build and run the application the index page will run
type this in the url to run time
http://localhost:60305/Home/Gettime
you will this output
Now add action name attribute in the controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DemoStoreproc.Models;
using System.Data;
using System.Data.Entity;
namespace DemoStoreproc.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
var context = new DemoEntities();
ViewBag.Country = context.CountryLists;
ViewBag.users = context.RegistrationForms.ToList();
return View();
}
[ActionName('CurrentTime')]
public string Gettime()
{
return DateTime.Now.ToString();
}
}
}
Again run the application and type http://localhost:60305/Home/Gettime
in the url you will get this error beacause I have applied action name with
CurrentTime.
Now add action name attribute in the controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DemoStoreproc.Models;
using System.Data;
using System.Data.Entity;
namespace DemoStoreproc.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
var context = new DemoEntities();
ViewBag.Country = context.CountryLists;
ViewBag.users = context.RegistrationForms.ToList();
return View();
}
[ActionName('CurrentTime')]
public string Gettime()
{
return DateTime.Now.ToString();
}
}
}
Again run the application and type http://localhost:60305/Home/Gettime
in the url you will get this error beacause I have applied action name with
CurrentTime.
Now change the
http://localhost:60305/Home/CurrentTime
Non Action
Non action attribute that is use in a controller when we want that a public method of controller that it should not be treated as action method.
Now I will take an example to explain Non action . This example will make clear the use of Non action
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DemoStoreproc.Models;
using System.Data;
using System.Data.Entity;
namespace DemoStoreproc.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
var context = new DemoEntities();
ViewBag.Country = context.CountryLists;
ViewBag.users = context.RegistrationForms.ToList();
return View();
}
[ActionName('CurrentTime')]
public string Gettime()
{
return Time();
}
[NonAction]
public string Time()
{
return DateTime.Now.ToString();
}
}
}
In the above example Time action cannot be use in the url beacause it has the attribute with NonAction so it will not be treated as action method.