Understand routing in MVC application

Sourav.Kayal
Posted by in ASP.NET MVC category on for Beginner level | Points: 250 | Views : 3397 red flag

How to implement routing in MVC application

Understand routing in MVC application

In this article we will learn routing in MVC application. Routing concept is very primary concept of MVC application and every developer should know it to develop MVC application. Let’s create one view page and one controller at first. Here is simple code to create view.

Create view
@model MVCTest.Models.Person
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        This is Test Page...
    </div>
</body>
</html>

 

Create simple controller
public class PersonController : Controller
    {
        public ActionResult Index()
        {
            return View("Index");
        }
    }
 

The controller name is Person and it contains one action named Index and this action will call Index view which we have created at first.


Let’s start with routing

Routing happens in global.aspx page. To enable routing in application at first we have to enable routing in Application’s start event. In below we are starting routing within Application_Start event.

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }

Now, within RegisterRoutes() event we have to define collection of routing information. routes.MapRoute() is  collection where data stores in key value pair. We can add more than one routing collection here.

  public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
            routes.MapRoute("Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
             new { controller = "Person", action = "Index", id = UrlParameter.Optional }         // Parameter defaults
            );
 
        }
 

In above example we have defined single routing information. The route name is “Default” and the default URL pattern is in below format.

Controller/Action/id

And we are setting value using new operator. Here our default controller’s name is Person and default action is Index and we are specifying id is an optional parameter. Means when we will invoke this action the parameter value is optional.

Now, Lets run this application with proper URL. Here is sample output.


The concept is, at the start time of application Default route get call, means if we do not call any particular action then by default the action will get call which is defined in Default route.

How to change the sequence of routing information?

We can change the sequence in our own. Have a look on below code.

routes.MapRoute(            
"Default", // Route name
                "{action}/{controller}/{id}", // URL with parameters
                new { action = "Index", controller = "Person", id = UrlParameter.Optional } // Parameter defaults
            );


Here we have defined action at first then the controller. So the sequence is.

Action/Controller/id

And the corresponding value is

Action=”Index”, controller=”Person”, id=”UrlParemeter.Optional”

So we are passing at first action name and then controller name. Here is sample output.


Conclusion:-

In this article we have discuss basic routing concept in MVC application.  Hope you have understood the basic concept and in coming up articles we will discuss more on same topic.

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)