Routing Concepts of ASP.NET MVC Framework

By
ASP.NET MVC Framework works on the concepts of routing to map URLs to the Controller class and Action defined in the Controller.Routing use the Route table for Matching the URL definition according to a pattern in mentioned Route table and passes them as parameter arguments to a controller Action after validation.
ASP.NET application, not using routing concepts.It specifically maps the URL based incoming request to the physical file(ex .aspx file) .
routing table

You can see Routing table codes in your Global.aspx file as given below:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Student_MVC_Application
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

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

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
        }
    }
}

Description:- Here you can see following routing elements as given below:-
  • Route Name = Default
  • URL with Parameter = {controller}/{action}/{id}
  • Controller Name = Mystudent
  • Action Name = Index
  • MapRoute and IgnoreRoute are use as extension methods of the RouteCollection class.
  • The MapRoute makes use of the Route Object property and creates an instance of Route.
  • The IgnoreRoute method works with the StopRouteHandler Class.
  • {resource}.axd/{*pathInfo}, is used to prevent requests for the web resources,such as WebReference.axd or ScriptResource.axd ,from getting passed to a controller.
What is Pattern 
A pattern is a signature that helps for matching the Incoming Request to your system or other.
Example :-
{controller}/{action}/{id} is a predefined pattern in every MVC templates.You can change your route Path according to your project using Custom Routing Functionality.
What is the Meaning of this Pattern:-
Suppose you have hosted your MVC Application on server and your domain name is http://mysite.com .If you want to open a page that contains images and image id 10 then what will you do? You will find this page through your Browser URL request over the internet.
You will type your request as following way in your browser as given below:- http://mysite.com/controller-name/action-name/id-parameter .
Suppose your project definition as follows.
 controller name = Bollywood
action-name =images
Image id=10
Then you will request following URL through browser over internet as given below:-
 http://mysite.com/Bollywood/images/10
  Note:-
  • If You want access above information on your local system then it will be like this http://localhost:59279/Bollywood/images/10
  • Remember, Name of controller and action will be case sensitive.
  • Remember one things,Route name can't duplicated in application.it will be unique in whole application.
  List of URLs and its Meaning:-
There are following URLs and its definitions as given below:-
1.) http://mysite.com  :- 
Here controller =Home, action=Index ,id =none, since default value of controller and action are Home and Index Respectively.
2.) http://mysite.com/Bollywood :-
Here controller =Bollywood, action=Index ,id =none ,since default value of action in Index.
3.) http://mysite.com/Bollywood/images :-
Here controller =Bollywood, action=images ,id=none
4.) http://mysite.com/Bollywood/images/10 :-
Here controller =Bollywood, action=images ,id=10
5.) http://mysite.com/Bollywood/images/Amir/10 :-
There is no match found because this pattern route is not defined in Global.asax file .First You have to define a separate route for this URL in Global.aspx file.Then it will work .

Now one question will be  raised in your mind "How to create custom route in ASP.NET MVC application".I will explain it in a separate post.
 For More...
  1. Binding concepts of WPF
  2. How to implement Role based security in asp.net application
  3. How to display xml data in list box in asp.net application
  4. Advance .net and c# interview questions and answers
  5. How to insert data in ms access database and display it in gridview control
  6. How to save image on website folder in asp.net

0 comments:

Post a Comment

Powered by Blogger.