Navigation

Tuesday 18 September 2018

ASP.NET Routing(URL Rewriting ) with the help of Global.asax

Simply add a Global.asax page in our existing Asp.net project. Add System.Web.Routing namespace and write the following code on your global file. 



--------------------Start of Global.asax page-----------------------------------------------

<%@ 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);
    }
    public static void RegisterRoutes(RouteCollection routeCollection)
    {
        // Id is the parameter value that we can pass in url.
        routeCollection.MapPageRoute("RouteForArticle", "Reg/{Id}", "~/MemberReg.aspx");
    }
    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.

    }
      

</script>
---------------------------------End of Global.asax page-------------------------------------


The code of default.aspx.cs page


//We can get Id value by the following code.


if (this.Page.RouteData.Values["Id"] != null)
     {
         ViewState["Id"] = this.Page.RouteData.Values["Id"].ToString();
     }

No comments:

Post a Comment