Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> MVC >> Routing in ASP.NET MVC
Routing in ASP.NET MVC
Routing in ASP.NET MVC


by Rohit Kesharwani on 10/1/2011 3:04:53 PM

Views: 2467       Comments: 0

Routing in ASP.NET MVC

A route is a URL pattern that is mapped to a handler. The handler can be a physical file, such as an .aspx file in a Web Forms application. Routing module is responsible for mapping incoming browser requests to particular MVC controller actions.
MVC3 routes are defined in the Global.asax.cs file of your MVC3 web application. By default, there is only one route defined in the RegisterRoutes method that looks like the line below:

          public static void RegisterRoutes(RouteCollection routes)

       {

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("Default", // Route name

            "{controller}/{action}/{id}", // URL with parameters

            new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
                                                                // Parameter defaults

            );

       }

This route defines the route name, which can be anything but must be unique, the URL template, and the parameter defaults. The default route that is pre-defined for you maps to Controller/Action/id. In the above, the MapRoute function will create a System.Web.Routing.Route instance and add it to routes collection. A route will determine whether a URL is matched with its definition and which controller will process the URL.

HomeController.cs

using System.Web.Mvc;

 

namespace MVCApplication.Controllers

{

    public class HomeController : Controller //HomeController is the name of controller

    {

        public ActionResult Index(string id) //Index is the name of Action and id is the
                                               optional parameter

        {

            return View();

        }

    }

}

 


You can add additional routes by copying the same line and adjusting the URL parameters and the related default values.

        public static void RegisterRoutes(RouteCollection routes)

        {

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("Default", // Route name

            "{controller}/{action}/{id}", // URL with parameters

            new { controller = "Home", action = "Index", id = UrlParameter.Optional } //
                                                                  Parameter defaults

            );

            routes.MapRoute(

                "MyRoute",  // Route name

                "{controller}/{action}/{Category}", // URL with parameters

                new { controller = "Home", action = "Product", Category = (string)null } 
                                  // Parameter define controller,action and urlparameter

            );

        }

The above custom route map defines Home/Product/Category. Home denotes a Controller, Product denotes an Action method in controller and Category is parameter through which we can send category value along with URL.

When an MVC application first starts, the Application_Start() method is called. This method, in turn, calls the RegisterRoutes() method. The RegisterRoutes() method creates the route table. Initially, single route created in a route table and if we create as many as routes it will be added in the route table.

protected void Application_Start()

{

     AreaRegistration.RegisterAllAreas();

     RegisterRoutes(RouteTable.Routes);

}

 

How to send the route parameter through URL?

1) Create Index view and write sample code as given below and also create Product view 
    too.

<html>

<head runat="server">

    <title>Index</title>

</head>

<body>

    <div>

        <h2>

            <a href="Home/Product/2">Product</a>

        </h2>

    </div>

</body>

</html>


2) Create a Controller as below:

using System.Web.Mvc;

namespace MVCApplication.Controllers

{

    public class HomeController : Controller

    {

        public ActionResult Index(string id)

        {

            return View();

        }

        public ActionResult Product(int id)

        {

            Response.Write(“Category Value: +id);

            return View();

        }

    }

}

Routing in ASP.NET MVC

Routing in ASP.NET MVC
The above example shows how can we pass route parameter through URL utilize its value in another View.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Rohit KesharwaniRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 4780
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2014MindStick. All Rights Reserved.