Here we will learn what are the action methods in asp.net mvc and how to define URLs for action method in asp.net mvc with example.
Let’s start understanding what the Action method is? When any user wants to access a website or application in asp.net mvc user will enter URL in the browser like as shown below.
E.g., http://localhost:7575/PersonDetails/Index
In the above URL, you can see that PersonDetails is controller, and Index is an action method in which the user is entering to invoke the action method index by entering the mentioned URL in the browser.
URL | Controller | Action | ID |
---|---|---|---|
/PersonDetails/Index | PersonDetails | Index |
When the controller gets a request by the browser, the controller will invoke the method inside of it. If the method is not found in the controller, it will popup error, saying HTTP has not found an exception.
The method with ActionResult in asp.net mvc is called Action Methods, and it will return various View Results. For example, the following is the PersonDetailsController code snippet. You will see that the Controller is a Class, and Index is a Method inside that class. Here Index is the action method of PersonDetailsController.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tutorial3.Models;
namespace Tutorial3.Controllers
{
public class PersonDetailsController : Controller
{
//
// GET: /PersonDetails/
[HttpGet]
public ActionResult Index()
{
return View(newPerson());
}
}
}
This is how we can define action methods and URLs for action methods in asp.net mvc application.