Friday, 7 July 2017

URL Routing

URL Routing In ASP .Net  :

How to implement URL Routing please follow the below steps.

Step 1 : Add  Global.asax file in your project


























Step 2 :  Open the Global.asax file and import the namespace
<%@ Import Namespace="System.Web.Routing" %> show the below code.

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RegisterRoutes(RouteTable.Routes);

    }
   
    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.

    }
    static void RegisterRoutes(RouteCollection routes)
    {
        //routes.MapPageRoute("test1abcd", "test12311", "~/Test1.aspx");
         routes.MapPageRoute("test2pqr", "test456", "~/Test2.aspx");
         routes.MapPageRoute("test3lmn", "test45678", "~/Test3.aspx");
    }
      
</script>



Step 3 : I have created a method named  RegisterRoutes and it has been called inside the Application_Start event inside the Global.asax file .Please check above code.

Step 4 : If we redirect the page from one page to another write the below code  on the click event.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Test2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Response.Redirect("Test3.aspx");
        Response.RedirectToRoute("test3lmn");
    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Routing;

public partial class Test1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       
        Response.RedirectToRoute("test2pqr");
      
    }
}


Step 5 : After write the redirect code then run the application URL shows like  below screen.



No comments:

Post a Comment