What is MVC (Model view controller) routing (Tutorial number 7)?

Questpond
Posted by in ASP.NET MVC category on for Advance level | Points: 250 | Views : 29148 red flag
Rating: 4 out of 5  
 1 vote(s)

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
 

Article description

MVC ( Model view controller) videos

MVC ( Model view controller) Article links

A simple Hello world ASP.NET MVC ( Model view controller) application.

Hello word MVC Tutorial video

http://www.dotnetfunda.com/articles/article1297-how-to-create-a-simple-hello-world-aspnet-mvc-tutorial-no-1-.aspx 

In this video we will see how we can share data between controller and the view using view data.

MVC Tutorial video 2 Viewdata

http://www.dotnetfunda.com/articles/article1310-how-to-pass-data-from-controllers-to-views-in-aspnet-mvc-tutorial-no-2-.aspx 

In this article we will create a simple customer model, flourish the same with some data and display the same in a view.

MVC Tutorial video 3 Simple model and view example

http://www.dotnetfunda.com/articles/article1317-how-to-create-a-simple-model-in-aspnet-mvc-and-display-the-same-tutorial-.aspx

In this article we will create a simple customer data entry screen with some validation on the view.

MVC tutorial video 4 Simple customer data entry screen.

http://www.dotnetfunda.com/articles/article1388-how-to-create-a-simple-aspnet-mvc-data-entry-screen-tutorial-no-4-.aspx 

This article will demonstrate how to expedite your MVC development process using HTML helper classes.

NA

http://www.dotnetfunda.com/articles/article1415-how-to-create-mvc-model-view-controller-views-faster-by-using-html-helper-.aspx 

This article demonstrates how we can do unit testing in MVC (Model view controller) applications.

NA

http://www.dotnetfunda.com/articles/article1429-how-can-we-do-unit-test-in-mvc-model-view-controller-application-tutoria-.aspx


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.
 

Page copy protected against web site content infringement by Copyscape

Login to vote for this post.

Comments or Responses

Posted by: Vinay13mar on: 10/14/2012 | Points: 25
wonder full explanation. thanks , i have found one more link when i was surfing.
http://dotnetpools.com/Article/ArticleDetiail/?articleId=37&title=What%20is%20MVC?

Login to post response

Comment using Facebook(Author doesn't get notification)