Amazon Ad

Saturday 14 September 2013

Url Routing For WebForms in ASP.NET 4.5

Hi guys,

Today i am going to tell you how to use URL Routing for ASP.NET Webforms in .net framework 4.0 and 4.5.

Step 1 : Your global.asax file should be like

public class Global : System.Web.HttpApplication
    {

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

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapPageRoute("Default", "ritesh", "~/Default.aspx");
            routes.MapPageRoute("DefaultWithIdParam", "ritesh/{id}", "~/Default.aspx");
        }
    }

Step 2 : Create a page Default.aspx if it doesnt exists. Add the following lines on page_load event

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string data=Page.RouteData.Values["id"].ToString();
            }
            catch (Exception ex)
            {

            }
        }

Step 3: In browser type http://localhost:{port}/ritesh and http://localhost:{port}/ritesh/23. This would return you the page and the data also.

Thanks
Ritesh

No comments:

Post a Comment

Comments are welcome, Please join me on my Linked In account

http://in.linkedin.com/pub/ritesh-tandon/21/644/33b

How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...