Skip to main content

MVC – Action Verbs

  MVC – Action Verbs

Action Selectors
·         Action selector is the attribute that can be applied to the action methods. It helps routing engine to select the correct action method to handle a particular request. MVC 5 includes the following action selector attributes:
o    ActionName
o    NonAction
o    ActionVerbs
ActionName
·         ActionName attribute allows us to specify a different action name than the method name.
public class EmployeeController : Controller
{

public EmployeeController()
{

}

[ActionName("find")]
public ActionResult GetById(int id)
{
// get employee from the database
return View();
}
}

·         In the above example, we have applied ActioName("find") attribute to GetById action method. So now, action name is "find" instead of "GetById". This action method will be invoked on http://localhost/employee/find/1 request instead of http://localhost/employee/getbyid/1 request.
public class EmployeeController : Controller
{
public EmployeeController()
{

}

[NonAction]
public Employee GetEmployee(int id)
{
return employeeList.Where(s => s.EmployeeId == id).FirstOrDefault();
}
}

·         This ActionName 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 attribute is used to change the name of action method.




[HttpGet], [HttpPost], [HttpPut], [HttpDelete] Attributes in Action Selectors
·         [HttpGet] - It is used to restrict an action method to handle only HTTP GET requests.
·         [HttpPost] - It is used to restrict an action method to handle only HTTP POST requests.
·         [HttpPut] - It is used to restrict an action method to handle only HTTP PUT requests.
·         [HttpDelete] - It is used to restrict an action method to handle only HTTP DELETE requests.
ActionVerbs
·         The ActionVerbs selector is used when you want to control the selection of an action method based on a Http request method. For example, you can define two different action methods with the same name but one action method responds to an HTTP Get request and another action method responds to an HTTP Post request.
Http method
Usage
GET
To retrieve the information from the server. Parameters will be appended in the query string.
POST
To create a new resource.
PUT
To update an existing resource.
HEAD
Identical to GET except that server do not return message body.
OPTIONS
OPTIONS method represents a request for information about the communication options supported by web server.
DELETE
To delete an existing resource.
PATCH
To full or partial update the resource.

public class EmployeeController : Controller
{
public ActionResult Index()
{
return View();
}

[HttpPost]
public ActionResult PostAction()
{
return View("Index");
}


[HttpPut]
public ActionResult PutAction()
{
return View("Index");
}

[HttpDelete]
public ActionResult DeleteAction()
{
return View("Index");
}

[HttpHead]
public ActionResult HeadAction()
{
return View("Index");
}

[HttpOptions]
public ActionResult OptionsAction()
{
return View("Index");
}

[HttpPatch]
public ActionResult PatchAction()
{
return View("Index");
}
}

public class HomeController : Controller
{

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetEmployee()
{
var p = new EmployeeModel();
return View("Employee", p);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Insert(EmployeeModel p)
{
return View("Employee");
}

[AcceptVerbs(HttpVerbs.Put)]
public ActionResult Update(EmployeeModel p)
{
return View("Employee");
}

[AcceptVerbs(HttpVerbs.Delete)]
public ActionResult Delete(int ID)
{
return View("Employee");
}
}


Comments

Popular posts from this blog

What is cookie? Advantages and disadvantages of cookies?

What is cookie? A cookie is a small piece of text file stored on user's computer in the form of name-value pair. Cookies are used by websites to keep track of visitors e.g. to keep user information like username etc. If any web application using cookies, Server send cookies and client browser will store it. The browser then returns the cookie to the server at the next time the page is requested. The most common example of using a cookie is to store User information, User preferences, Password Remember Option etc.It is also one of the common and mostly asked interview questions. Some facts about Cookie Here are a few facts to know about cookies: · Cookies are domain specific i.e. a domain cannot read or write to a cookie created by another domain. This is done by the browser for security purpose. · Cookies are browser specific. Each browser stores the cookies in a different location. The cookies are browser specific and so a cookie created in one browser(e.g in Google Chrome

Code First Getting Started

In this tutorial let us create a simple application to demonstrate the use of entity framework using code first. We are using Visual Studio 2015 and entity framework 6.1.3. You can download Visual Studio community Edition . You should have the basic knowledge of .Net framework, C# and MS SQL Server. In this tutorial, we will create a simple application with a user class.  Our user class will have basic information like name and email address of the user. Create the Project Open Visual Studio. File ->New -> Project Select C# -> Select Console Application Name the application as “EFGettingStarted” Click on OK Install Entity Framework The next step is to install the Entity framework. This can be installed via nuget package console. Click on Tools->Nuget Package manager -> Package Manager Console and type the following command C# 1 2 3   install - package entityframework   This will install the late

First, FirstOrDefault, Single, SingleOrDefault In C#

For people who are new to LINQ, it is difficult to understand the difference between First, FirstOrDefault, Single, SingleOrDefault. In this blog, I will explain what to use and when.     I will take a simple example to make you understand practically how these methods work.   Consider a class Employee with properties as Id, Name, and Department. class  Employee {    public   int  Id {  get ;  set ; }    public   string  Name {  get ;  set ; }    public   string  Department{  get ;  set ; } } I have a list of Employees: List<Employee> employeeList =  new  List<Employee>(){    new  Employee() { Id = 1, Name =  "Sunny" , Department =  "Technical"  },    new  Employee() { Id=2, Name= "Pinki" , Department = "HR" },    new  Employee() { Id=3, Name= "Tensy" , Department = "Finance" },    new  Employee() { Id=4, Name= "Bobby" , Department = "Technical" },    new