This is the 7th MVC (Model view controller) tutorial and in this article we will look what is MVC routing. In case you are completely new to MVC (Model view controller), do have a look at the previous articles and videos given in the table below for quick start
What is MVC (Model
view controller) routing (Tutorial number 7)?
Introduction
This is the 7th MVC (Model view controller) tutorial and in this article we
will look what is MVC routing. In case you are completely new to MVC (Model
view controller), do have a look at the previous articles and videos given in
the table below for quick start
My half life has been spent on writing interview questions for Microsoft
technology and I hope I keep writing them. You can see my video tutorial for
the same by clicking on .NET,ASP.NET,C#,MVC
Interview questions and answers
At the end of the day MVC is nothing but URL mapped to controllers and controllers
mapped to actions.
For example when a user sends a request URL like www.questpond.com/locateproduct
from the browser, these actions are mapped with MVC controllers and MVC
controllers finally invokes those functions.
Below is a simple table which shows how the whole thing looks like.
Adding further to the complication we can have multiple URL’s mapped to one
controller or you can have more than one controller mapped to a single URL. For
instance you can have www.questpond.com/contactus
and www.questpond.com/aboutus
mapped to a single controller called as “AboutUsController”.
It would be great if we have some kind of mechanism by which we can configure these
mappings. That’s what exactly MVC routing is meant for. MVC routing helps to
easily configure and map the URL with the controllers
Step 1:- Take the MVC project defined in the previous article / tutorial.
Let’s take the same customer project we had discussed in the previous
section.
Step 2 :- Change global.asax.cs
All route mappings are stored in the “global.asax.cs” behind code file. So
the first step is we need to go and change this file.
All routing mapping are stored in to a collection called as ‘routes’. This
collection belongs to the namespace “System.Web.Routing”. To add a route you
need to call the ‘MapRoute’ method and pass three parameters “name”,”url” and
“defaults”.
Below is a print screen of the snippet of the ‘maproute’ function.
“Name” is the key name by which the route will be identified from the
collection.
“Url” defines what kind of URL format we want to connect with the controllers.
For instance in the below code snippet we are saying that “View/ViewCustomer”
is the URL format.
“Defaults” defines the controller class and action functions which will be
invoked when the URL is called. For instance in the below code we are saying
that when “View/ViewCustomer” is called it will invoke the “Customer”
controller class and the action function invoked will be “DisplayCustomer”.
In case your controller takes parameters you can use the “{“brackets. For
instance in the below code snippet we have used “{“to specify that we can have
“id” parameter.
If you want to define the parameter as optional you can use the
“UrlParameter.Optional” enum.
The first thing is comment the default mapping code. We will explain the default
mapping code later.
//routes.MapRoute(
// "Default", // Route name
// "{controller}/{action}/{id}", // URL with parameters
// new { controller = "Home", action = "Index", id
= UrlParameter.Optional }); // Parameter defaults
Put the below code, which means when we call http://localhost/View/ViewCustomer/
it will invoke the customer controller and will call displaycustomer function.
routes.MapRoute(
"View", // Route name
"View/ViewCustomer/{id}", // URL with parameters
new { controller = "Customer", action = "DisplayCustomer",
id = UrlParameter.Optional }); // Parameter defaults
Below is the action function “DisplayCustomer” which will be invoked.
public ViewResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = 12;
objCustomer.CustomerCode = "1001";
objCustomer.Amount = 90.34;
return View("DisplayCustomer",objCustomer);
}
Step 3:- Run the application
If you run the application you should see the below display.
If you remember we commented the default entry route. Let’s understand what
exactly this default code meant.
"{controller}/{action}/{id}" defines that URL will be
automatically named with the convention of controller name / function action
name / value. So if you have a controller class with ‘Customer” and action
function as “Search” then the URL will be structured as http://xyz.com/Customer/Search
automatically.
//routes.MapRoute(
// "Default", // Route name
// "{controller}/{action}/{id}", // URL with parameters
// new { controller = "Home", action = "Index", id =
UrlParameter.Optional }); // Parameter defaults
So what’s in the next article
In the next lab we will discuss how to validate MVC URL. All actions to MVC
come via MVC URL and even data is fed via MVC URL. So in the next article we
will see how we can validate the URL data.