Tuesday, 28 January 2014

Routing in Asp.Net MVC with example in mvc4


Description:-

            In previous example we explain that how to display dates using DayCalenderPage extention method Displaying dates using page calender Extention method in mvc4 but now in this example we explain the how to create Routing in mvc.

What is Routing in mvc:-

Basically, Routing is one kind of a pattern and are used to matching system that monitor the incoming request and figure out what to do with that request

Why use routing?

when we are developing an eCommerce application to sell products online we use URL's like :http://www.onlineshopping.com/electronics.aspx.

If we observe the above URL  we can easily say that electronics.aspx is an ASP.NET page that display the list of the electronic products. So this is self explanatory. Now we want to add a subcategory called mp3 that will display only the MP3 players to the user instead of all the electronic products. Simple we apply the filter on the page and when the user clicks the mp3 hyperlink on the page we pass a unique id in the URL that represents the mp3 player. So our problem is solved. Now our URL looks like http://www. onlineshopping.com/electronics.aspx?catId=8. But if we are having hundred’s of products to sell. So we end up with URL’s like

www. onlineshopping.com/electronics.aspx?catId=8
www. onlineshopping.com/electronics.aspx?catId=9
www. onlineshopping.com/electronics.aspx?catId=15

The above URL’s appears to be just perfect for us and syntactically they are, but thinking from the end user’s perspective which product URL you think he will remember more than the others. I could say none of the above URLs. We are after all humans and we are more likely to remember words than numbers. So all the above URLs are the same for the end user. Wouldn’t it be nice if we have URLs like:

www.myproducts.com/electronics/products/mp3
www.myproducts.com/electronics/products/tv
www.myproducts.com/electronics/products/laptop

The above URLs seem self explanatory and the user can easily guess that if we sell CDs he just have to append cd at the end of the common URL

www.myproducts.com/electronics/products/cd  

These types of URL’s are called hackable URL’s and it is one of the advantages of using routing
How to Define or Register Route






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 you 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. I am sharing a list of URLs that match and don't match this route pattern.



insert,update,delete in gridview without Postback CRUD operation without Postback in asp.net

Dynamically Bind data in Ajax Accordion Panel Ajax Accordion panel Example in asp.net

how to Restrict the size of file upload when upload by user restrict the size of file upload control in asp.net




RouteConfig.cs:-

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

namespace routeurl
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
              name: "team",
              url: "team",
              defaults: new { controller = "Home", action = "about", id = UrlParameter.Optional }
          );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
          
        }
    }

}
This entry was posted in :

0 comments:

Post a Comment