The routing engine is responsible for getting the incoming request and routing that request to the appropriate Controller based on the URL pattern. We can configure the routing engine so that it can choose the appropriate controller based on the relevant information.
By convention, ASP.NET MVC follows this pattern: Controller/Action/Id.
If the user types the URL http://yourwebsite.com/Hello/Greeting/1
, the routing engine selects the Hello controller
class and Greeting action
method within the HelloController
, and passes the Id
value as 1
. You can give default values to some of the parameters and make some of the parameters optional.
The following is the sample configuration:
The template: "{controller=Hello}/{action=Greeting}/{id?}");
In the preceding configuration, we are giving three instructions to the routing engine:
controller/action/id
.Hello
and Greeting
for the controller
and action
respectively, if the values for controller
or action
are not supplied in the URL.Id
parameter optional so that the URL does not need to have this information. If the URL contains this Id
information, it will use it. Otherwise, the Id
information will not be passed to the action
method.Let us discuss how the routing engine selects the controller
classes, action
methods, and Id
values for different URLs:
URL1:http://localhost/ Controller: Hello Action method: Greeting Id: no value is passed for the id parameter
Reasoning: The Hello
controller is passed as the default value as per the routing configuration, as no value is passed as the Controller in the URL.
The following action
method will be picked up by the routing handler when the preceding URL is passed:
public class HelloController : Controller { public ActionResult Greeting(int id) { return View(); } } URL2: http://localhost/Hello/Greeting2 Controller: Hello Action method: Greeting2 Id: no value is passed for the id parameter
Reasoning: The Hello
controller will be chosen as the URL contains Hello
as the first parameter, and the Greeting2
action method will be chosen as the URL contains Greeting2
as the second parameter. Please note that the default value mentioned in the configuration would be picked only when no value is present in the URL. As the id
parameter is optional and the URL does not contain the value for id
, no value is passed to the id
parameter.
The following action method Greeting2
will be picked up by the routing handler when the preceding URL is passed:
public class HelloController : Controller { public ActionResult Greeting(int id) { return View(); } public ActionResult Greeting2(int id) { return View(); } } URL3: http://localhost/Hello2/Greeting2 Controller: Hello2 Action method: Greeting2 Id: no value is passed for the id parameter
Reasoning: As Hello2
is passed as the first parameter, the Hello2
controller will be selected, and Greeting2
is the action method selected since Greeting2
is passed as the second parameter. As the id
parameter is optional and no value is passed for the parameter id
, no value will be passed for the id
.
The following action
method will be picked up by the routing handler when the preceding URL is passed:
public class Hello2Controller : Controller { public ActionResult Greeting2(int id) { return View(); } } URL4: http://localhost/Hello3/Greeting2/1 Controller: Hello3 Action method: Greeting2 Id: 1
Reasoning: Hello3
is the controller selected as it is mentioned as the first parameter, Greeting4
is the action method, and 1
is the value passed as the id
.
The following action
method will be picked up the routing handler when the preceding URL is passed:
public class Hello3Controller : Controller { public ActionResult Greeting2(int id) { return View(); } }
We will discuss routing in detail in a later chapter.
Once the request reaches the controller, the controller will create a response by talking to the Model and may pass the data to View and the View will then be rendered to the end user.