- Routing is a pattern matching system
- Routing maps incoming request (from browser) to a particular resource(controller and action Method)
Every Request come routing match Route Table then call particular controller
Route Table
We define route for each action method
All the routes are stored in route table
Each incoming request mapped to this route table
There are 2 type use Routing in MVC Application
Traditional Way
Attribute routing
Traditional Way
In a traditional way routing define route particular file routeconfig.cs with in app-start folder
Routeconfig.cs Code
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "allstudents",
url: "students",
defaults:new {controller= "Student" ,action= "GetAllStudent" }
);
routes.MapRoute(
name: "Students",
url: "students/{id}",
defaults: new { controller = "Student", action = "GetSingleStudent" },
constraints: new { id = @"\d+" }
);
routes.MapRoute(
name: "StudentsAddress",
url: "students/{id}/Address",
defaults: new { controller = "Student", action = "StudentAddress" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}