articles

URL Routing in MVC 4

Anonymous User10420 10-Feb-2015

Hi everyone in this article I’m discus about URL Routing. How to manage URL Routing.

In my previous post I’ll intruduced about Working With Controller in ASP.NET MVC 4

Description:


ASP.NET MVC Framework and ASP.NET Dynamic data extends to routing to provide features that are use only in mvc application and in dynamic data applications.

The function of routing system can be divided in two parts

1.       RouteData: This mechanism is used to examine an incoming url and then decide which controller and action the request is need to be send.

2.      VirtualPath: This mechanism is responsible for generating outbound url. Theae are the URLs that appear in the html render from our views so that a specific action will be invoked when user click the link.

URL Routing is directly related to Controller class. In my previous post I’ll explain aboutWorking with Controller.

Routes:

A route is a URL pattern that is mapped to a handler. The handler can be physical file such as an .aspx file in a web forms application. A handler can also be a class that processes the request, such as a controller in an mvc application to define a route you create an instance of route class by specifying the URL pattern.

Basically routing is a pattern matching system that monitor the incoming url and figure out what to do with that request. At runtime routing engine use the Route table for matching the incoming requests url patterns against the url pattern defined in the route table. You can register one or more url patterns in to the route table at Application_Start event. MVC 5also support attribute routing.

How to define route:

 

publicclassRouteConfig
{
    publicstaticvoid RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
        routes.MapRoute(
            name: "", // Route Name
            url: "{controller}/{action}/{id}", //Route Pattern
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Default Values for above define parameters
        );
    }
}

 

RouteConfig.RegisterRoutes(RouteTable.Routes); 

When the routing engine finds a match in the route table for the incoming request's URL, it forwards the request to the appropriate controller and action. If there is no match in the route table for the incoming request's URL, it returns a 404 HTTP status code.

In above example we have defined the Route Pattern {controller}/{action}/{id} and also provide the default values for controller, action and id parameters. Default values means if you will not provide the values for controller or action or id defined in the pattern then these values will be serve by the routing system.

Suppose your webapplication is running on www.example.com then the url pattren for your application will be www.example.com/{controller}/{action}/{id}. Hence you need to provide the controller name followed by action name and id if it is required. If you will not provide any of the value then default values of these parameters will be provided by the routing system. Here is a list of URLs that match and don't match this route pattern.

1.       http://www.example.com/ :-  In this url we have provide default controller name and default action name (default Controller=Home, default action= Index, id=none)


       URL Routing in MVC 4

2.       http://www.example.com/Admin :-  In this url we have provide Admincontroller name and default action name of Admin controller (Controller=Admin, default action= Index, id=none)          


URL Routing in MVC 4

3.  http://www.example.com/Admin/NewUser :-  In this url we have provide Admincontroller name and NewUser action name of Admin controller (Controller=Admin, default action= NewUser, id=none)           


URL Routing in MVC 4

4.    http://www.example.com/Admin/NewUser/886 :-  In this url we have provide Admincontroller name and NewUser action name of Admin controller and pass id parameter (Controller=Admin, default action= NewUser, id=886)

5.   http://www.example.com/Admin/NewUser/Add/1 :-  This url is not match. 


    URL Routing in MVC 4

 URL Routing in MVC 4

 

Difference between Routing & URL Rewriting:

Many developers compare Routing to URL Rewriting that is wrong Routing is other things and URL Rewriting is other things. Below is the main difference between Routing and URL Rewriting.

1.       URL Rewriting is focused on mapping on URL while Routing is focused on mapping a URL to a resource.

2.       URL Rewriting rewrites your old url to new url while Routing never rewrite your old url to new url, but it map to the original route.

 

I hope this post is enjoy to you in my next post I’ll discus about Working with View


I am a content writter !

Leave Comment

Comments

Liked By