MVC Routing
MVC Routing means define the route for every resource. When
request came for MVC application what to do with that request this is called as
MVC routing. Which resource you have to call for which request parameters all
are defined in Route table.
When application starts, we register all routes in route
table.
We can define the routes by 2 way:
1. Attribute Route
2. Traditional Routes
Attribute routes are added in MVC 5 before that only
traditional routes are used for MVC routing.
Attribute Route
Let’s first discuss Attribute Routes.
Attribute routes are defined for specific action or specific
Controller or specific areas. And when we use attribute route you have to add
one statement in RouteConfig.cs like below:
public static void RegisterRoutes(RouteCollection
routes)
{
routes.MapMvcAttributeRoutes();
}
After this statement you can write any attribute route in
any controller.
Let’s take first example
public class EmployeeController : Controller
{
[Route("SampleEmployee")]
public ActionResult
Employee()
{
return View();
}
}
In the above code snippet when request come with “/SampleEmployee”
as the URL, then this action method execute. This is action level
attribute based routing.
If you want to set start URL of MVC application, you can
simply add route with blank string like below:
public class EmployeeController : Controller
{
[Route("")]
public ActionResult
Employee()
{
return View();
}
}
You can add parameters for action methods using attribute based routing
like below
public class EmployeeController : Controller
{
[Route("SampleEmployee/{id}")]
public ActionResult
Employee(int id)
{
return View();
}
}
If there is 2 or three method accepting id parameter and one method
is not accepting parameter then you have to add id parameter is optional please
see below code:
public class EmployeeController : Controller
{
[Route("GetEmployeeList")]
public ActionResult
GetEmployeeList()
{
return View();
}
[Route(“DeleteEmployee/{id}")]
public ActionResult
DeleteEmployee(int id)
{
return View();
}
[Route("GetEmployee/{id}")]
public ActionResult
GetEmployee(int id)
{
return View();
}
}
In the above example if you want to make controller
level routing you can simply add Route attribute and make “Id” optional like
below:
[Route("{action}/{id?}")]
public class EmployeeController : Controller
{
public ActionResult
GetEmployeeList()
{
return View();
}
public ActionResult
DeleteEmployee(int id)
{
return View();
}
public ActionResult
GetEmployee(int id)
{
return View();
}
}
You can also set the RoutePrefix attribute for assigning
controller level routing like below:
[RoutePrefix("Employee")]
[Route("{action}/{id?}")]
public class EmployeeController : Controller
{
public ActionResult
GetEmployeeList() //Route is Employee/GetEmployeeList
{
return View();
}
public ActionResult
DeleteEmployee(int id) //Route is Employee/DeleteEmployee/2
{
return View();
}
public ActionResult
GetEmployee(int id) //Route is Employee/GetEmployee/2
{
return View();
}
}
You can also specify multiple parameters for action method
like below
[RoutePrefix("Employee")]
public class EmployeeController : Controller
{
[RoutePrefix("Employee/{id}/{obj}")]
public ActionResult GetEmployee(int id,string name) //Route
is Employee/GetEmployee/2/john
{
return View();
}
}
If you want to set the data type of the parameter in
attribute based routing we can do it easily, because there are some situation
with same parameter name with 2 methods like method overloading:
[Route("{action}/{id?}")]
public class EmployeeController : Controller
{
public ActionResult
GetEmployeeList(string id)
{
return View();
}
public ActionResult
GetEmployeeList(int id) {
return View();
}
}
In the above example I hit the URL like Employee/GetEmployeeList/kkk
then attribute based routing get confused which action method to be called, so
for that you have to mention parameter data type like below:
public class EmployeeController : Controller
{
[Route("{action}/{id:string}")]
public ActionResult
GetEmployeeList(string id)
{
return View();
}
[Route("{action}/{id:int}")]
public ActionResult
GetEmployeeList(int id) {
return View();
}
}
See below link for related
articles
1.
Getting Started with MVC
2. ViewBag, ViewData and TempData
3.
Folder Structure In MVC
4.
MVC Architecture Theory
5.
Data Annotations and Validations in MVC
6.
MVC Architecture: Traditional Routes
7.
MVC Architecture: Attribute based Routing
8.
Bundling and Minification in MVC
9.
AutoComplete in MVC
http://dotnet-root.blogspot.in/2016/10/autocomplete-using-aspnet-mvc-and-jquery.html
10.
Adding Controller, Model and Views
0 Comments