Monday, 7 July 2014

Url Routing in Asp.Net

Write below code in Global.asax :

      using System.Web.Routing;

       protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }

        void RegisterRoutes(RouteCollection routes)
        {
            //routes.MapPageRoute("Name of Route"
, "strict Name/{string/int}/{string/int}"
, "Actual Path to Redirect"
, false
, new RouteValueDictionary { { "ID", "12" }, { "BookName", "food" } });
          
             //  SET DEfault Values for RouteData using 
                          new RouteValueDictionary { { "ID", "12" }, { "BookName", "food" } })

            // you can Use strict Name in Url with directly write string.
                If you want variable string in urk then User {}  bracket

              routes.MapPageRoute("RouteBook", "Book/{ID}/{BookName}", "~/Webform1.aspx"
           ,false
          , new RouteValueDictionary { { "ID", "12" }, { "BookName", "food" } });

            //  Url will     http://localhost/Book/12/CSharpCoding

            routes.MapPageRoute("CategoryRoute"
                                             , "{Category}/{ID}"
                                            , "~/LoginForm.aspx");

            // Url Will     http://localhost/categoryName/12

        }





Get Querystring Value in Web Page :


string str = Page.RouteData.Values["BookName"] as string;

// "BookName"  = Name of the parameter you have passed while routing





No comments:

Post a Comment