ASP.NET Routing in ASP.NET MVC is the concept of mapping the incoming request (URL) with the existing resources
For Example :
The incoming request URL for a local application is http://localhost:2233/home/index
In the Application say we have configured the routing as "{controller}/{action}/{id}", so Home is the controller here and index is the action here, the id is optional in most cases, if id is not given then it will display the default value
In ASP.NET MVC the controllers should have the naming convention, in this case the controller's name should be HomeController, should end with 'Controller' suffix.
The Routing mechanism looks in the HomeController for the Action that we set already,
Here Home is Controller and the Action is Index
In the Home Controller we will return the View (That is Index) as Action Result
Sample Home Controller
public class HomeController : Controller
{
public ActionResult Index() { return View(); }
}
You can do some minimal changes to the view in the controller like setting the header, customized message etc.
Registering the route:
You need to register the route in the Global.asax.cs file to make available the configuration
Key Points:
1) We can implement the routing concept for both ASP.NET Web Forms and MVC
2) We can change the URL Patterns as we want, the typical pattern most of the developers use is {controller}/{action}/{id}
3) Naming default URL pattern into 'Default' or empty string '' is permissible
ASP.NET Routing Advanced Concepts
For Example :
The incoming request URL for a local application is http://localhost:2233/home/index
In the Application say we have configured the routing as "{controller}/{action}/{id}", so Home is the controller here and index is the action here, the id is optional in most cases, if id is not given then it will display the default value
In ASP.NET MVC the controllers should have the naming convention, in this case the controller's name should be HomeController, should end with 'Controller' suffix.
The Routing mechanism looks in the HomeController for the Action that we set already,
Here Home is Controller and the Action is Index
In the Home Controller we will return the View (That is Index) as Action Result
Sample Home Controller
public class HomeController : Controller
{
public ActionResult Index() { return View(); }
}
You can do some minimal changes to the view in the controller like setting the header, customized message etc.
Configuring Route
Routing
configuration happens in the RegisterRoutes method of RouteConfig.cs file in
the App_Start folder,
public
class
RouteConfig
{
public
static
void
RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{ controller = "Home", action = "Index", id = UrlParameter.Optional
}
);
}
}
IgnoreRoute, as the name says this will ignore the .axd
files from mapping,
.axd files are static files which are not present in the
disk so allowing those files may lead to problem
More details about IgnoreRoute can be found here
Defaults--> The default values needed to be shown if no proper URL is given.
MapRoute is the one we are going to analyse fully
We have the following attributes,
Name --> Name of the Route
URL --> Key part in routing
Registering the route:
You need to register the route in the Global.asax.cs file to make available the configuration
protected void Application_Start()
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
Key Points:
1) We can implement the routing concept for both ASP.NET Web Forms and MVC
2) We can change the URL Patterns as we want, the typical pattern most of the developers use is {controller}/{action}/{id}
3) Naming default URL pattern into 'Default' or empty string '' is permissible
ASP.NET Routing Advanced Concepts
Comments