ASP.NET URL Rewriting Example

  • by
ASP.NET URL Rewriting Example

This post explains about ASP.NET URL rewriting. ASP.NET URL Rewriting Example.

Create a new ASP.NET website and create following .aspx pages  in your website. You can use any existing project too. I’m using Visual studio 2012 and .net framework 4.5 .

  1. Default.aspx
  2. PageTwo.aspx
  3. BasicInfo.aspx
  4. GetData.aspx

And make a Global.asax file in your project. Here, the global.asax file is the most important file as it will be doing the URL Rewriting of our .aspx pages.

Code for Global.asax :

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web" %>
<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

}
static void RegisterRoutes(RouteCollection routes)
{
//routes.MapPageRoute("Uniquename", "Name to shown on Adddress bar", "Physical Path to the page");
routes.MapPageRoute("Home", "Home", "~/Default.aspx");
routes.MapPageRoute("Info", "Info/{Msg}", "~/PageTwo.aspx");
routes.MapPageRoute("GetData", "GetData/{Name}/{Color}/{City}", "~/GetData.aspx");
routes.MapPageRoute("BasicInfo", "Basics", "~/BasicInfo.aspx");

}
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>

The method RegisterRoutes(RouteCollection routes) performs the rewriting task over here.

static void RegisterRoutes(RouteCollection routes)
{
//routes.MapPageRoute(“Uniquename”, “Name to shown on Adddress bar/{QueryStringnameIfAny}/{AnotherQueryStringnameIfAny}”, “Physical Path to the page”);
}

Open Default.aspx file and edit it as below :

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>ASP.NET URL Rewriting</h2>
<asp:TextBox runat="server" ID="txtMessage" />
<br />
<asp:Button runat="server" ID="btnProceed" Text="Proceed to Next Page" OnClick="btnProceed_Click" />
<br />
<asp:Button runat="server" ID="btnBasics" Text="Goto Basic Page" OnClick="btnBasics_Click" />
</div>
</form>
</body>
</html>

Code file Default.aspx.cs :

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

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

}
protected void btnProceed_Click(object sender, EventArgs e)
{

// "Info" from global.asax file and the query string msg
Response.Redirect("Info/" + txtMessage.Text);
}
protected void btnBasics_Click(object sender, EventArgs e)
{

// "Basics" from global.asax file
Response.Redirect("Basics");
}
}

Edit PageTwo.aspx page as below :

PageTwo.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageTwo.aspx.cs" Inherits="PageTwo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" ID="lblMessage" />
</div>
</form>
</body>
</html>

It only contains a label to show our query string.

Code file for PageTwo.aspx file

PageTwo.aspx.cs:

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

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

//Prasing the Query string using RouteData key index.
lblMessage.Text = RouteData.Values["Msg"] as string;
}
}

Edit the BasicInfo.aspx page and edit it as following:

BasicInfo.aspx.cs:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BasicInfo.aspx.cs" Inherits="BasicInfo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td align="right">What's your Name? </td>
<td>
<asp:TextBox runat="server" ID="txtName" /></td>
</tr>
<tr>
<td align="right">Enter a color Code :</td>
<td>
<asp:TextBox runat="server" ID="txtColor" /></td>
</tr>
<tr>
<td align="right">Enter a city name :</td>
<td>
<asp:TextBox runat="server" ID="txtCity" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button runat="server" Text="Go back to previous Page" OnClick="btnHome_Click" ID="btnHome" />
&nbsp;
<asp:Button runat="server" Text="Proceed to Next Page" OnClick="btnGetData_Click" ID="btnGetData" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

This file contains three textboxes. One for entering user’s name, second for color and third for city name.

Code file for BasicInfo.aspx

BasicInfo.aspx.cs:

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

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

}
protected void btnHome_Click(object sender, EventArgs e)
{
Response.Redirect("home");
}
protected void btnGetData_Click(object sender, EventArgs e)
{
Response.Redirect("GetData/" + txtName.Text + "/" + txtColor.Text + "/" + txtCity.Text + "/");
}
}

The method btnGetData_Click is important.

protected void btnGetData_Click(object sender, EventArgs e)
{
Response.Redirect("GetData/" + txtName.Text + "/" + txtColor.Text + "/" + txtCity.Text);
}

Basically we are passing our query string but not in a regular format.

Edit the GetData.aspx page as below :

GetData.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetData.aspx.cs" Inherits="GetData" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1><asp:Label runat="server" ID="lblInfo" /></h1>
</div>
</form>
</body>
</html>

This page also as only one label to show the data from the query string.

Code for GetData.aspx file:

GetData.aspx.cs:

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

public partial class GetData : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// GetData/{Name}/{Color}/{City}
lblInfo.Text = "Your name is "+RouteData.Values["Name"].ToString() +

". You entered city as "+RouteData.Values["City"].ToString();
Color c = Color.FromName(RouteData.Values["Color"].ToString());
lblInfo.ForeColor = c;
}
}

 

Set the default.aspx page as your default website page and run it. Comment below if face any difficulty in my code.

Thanks.

 

 


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.