URL Rewriting
A URL rewriting is very important when you are running a community website where user posts articles, forum messages etc. URL rewriting is the process of intercepting an incoming Web request and automatically redirecting it to a different URL. Discusses the various techniques for implementing URL rewriting.
For example
In this example we create a database table with title, description and author name. Title link will be create in the asp .net GridView control. when we click on the title link it will be redirect to the dynamic page.
Create a table in database named as Articletable. Table looks like this.
CREATE TABLE [dbo].[Articletable](
[ID] [int] NOT NULL,
[Title] [varchar](200) NULL,
[Description] [varchar](400) NULL,
[Author] [varchar](50) NULL
)
Inserting data in the articletable.
INSERT INTO Articletable VALUES(1,'How to validate dropdownlist in asp.net','Here, we will learn how to validate a DropDownList in ASP.NET.','Rohatash Kumar');
GO
INSERT INTO Articletable VALUES(2,'Introduction to .NET Assemblies in VB.NET',' Here is a comprehensive introduction to .NET assemblies.','sunil Kumar');
go
INSERT INTO Articletable VALUES(3,'BinaryReader and BinaryWriter classes in VB.NET','In this article I will explain about BinaryReader and BinaryWriter Classes in VB.NET.','Deepak Kumar');
go
INSERT INTO Articletable VALUES(4,'StreamWriter class in VB.NET','This article shows how to create a new text file and write a string to it.','Rohatash Kumar');
go
select * from articletable;
OUTPUT

Table1
Now in ASP. NET
- Open Visual Studio.
- Add two webForm to your website, name it Gridview.aspx and dynamicpage.aspx.
Now drag and drop a GridView control from the Toolbox on the gridview.aspx page.
Gridview.aspx
The form looks like this.

Figure1
The ASP. NET code for the gridview.aspx page.|
<form id="form1" runat="server"> <div> <h1> <span style="color: #009900">URL Rewriting</span></h1> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Width="788px" Height="80px"> <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> <Columns> <asp:TemplateField HeaderText="Title"> <ItemTemplate> <asp:HyperLink ID="hlTitle" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Title")%>' NavigateUrl='<%#GenerateURL(DataBinder.Eval(Container.DataItem,"Title"), DataBinder.Eval(Container.DataItem,"Id"))%>'></asp:HyperLink> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Description"> <ItemTemplate>
<asp:Label
ID="lblDesc"
runat="server"
Text='<%#DataBinder.Eval(Container.DataItem,"Description")%>'></asp:Label> </ItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="Author"> <ItemTemplate> <asp:Label ID="lblauthor" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Author")%>' NavigateUrl='<%#GenerateURL(DataBinder.Eval(Container.DataItem,"Author"), DataBinder.Eval(Container.DataItem,"Id"))%>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> </asp:GridView>
</div> </form> |
.CS code
|
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { GridView1.DataSource = GetData(); GridView1.DataBind(); }
public static string GenerateURL(object Title, object strId) { string strTitle = Title.ToString();
//#region Generate SEO Friendly URL based on Title
strTitle = strTitle.Trim(); strTitle = strTitle.Trim('-');
strTitle = strTitle.ToLower(); char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray(); strTitle = strTitle.Replace("c#", "C-Sharp"); strTitle = strTitle.Replace("vb.net", "VB-Net"); strTitle = strTitle.Replace("asp.net", "Asp-Net"); strTitle = strTitle.Replace(".", "-"); for (int i = 0; i < chars.Length; i++) { string strChar = chars.GetValue(i).ToString(); if (strTitle.Contains(strChar)) { strTitle = strTitle.Replace(strChar, string.Empty); } } strTitle = strTitle.Replace(" ", "-");
strTitle = strTitle.Replace("--", "-"); strTitle = strTitle.Replace("---", "-"); strTitle = strTitle.Replace("----", "-"); strTitle = strTitle.Replace("-----", "-"); strTitle = strTitle.Replace("----", "-"); strTitle = strTitle.Replace("---", "-"); strTitle = strTitle.Replace("--", "-"); strTitle = strTitle.Trim(); strTitle = strTitle.Trim('-'); strTitle = "~/Article/" + strTitle + "-" + strId + ".aspx";
return strTitle; }
private DataTable GetData() { string strConn = ("Data Source=.; uid=sa; pwd=Password$2; database=userinfo"); SqlConnection conn = new SqlConnection(strConn); SqlDataAdapter da = new SqlDataAdapter("select Id,Title,Description,author from Articletable", conn); DataSet ds = new DataSet(); da.Fill(ds, "MyTestTable"); return ds.Tables["MyTestTable"]; } }
|
dynamicpage.aspx
The form looks like this.
Figure2
The ASP. NET code for the dynamicpage.aspx page.
|
<form id="form1" runat="server"> <div> <h1><img src="../Images/article.gif" />Article</h1> <b>Title:</b><asp:Label ID="lblTitle" runat="server" Text="Label" Font-Bold="true" ForeColor="blue"></asp:Label><br /> AuthorName <b>:</b><asp:Label ID="lblauthor" runat="server" Text="Label" Font-Bold="true" ForeColor="blue"></asp:Label> <br /> <br /> <b>Description:</b><br /> <asp:Label ID="lblDescription" runat="server" Text="Label"></asp:Label><br /><br /> <br /><br /> </div> </form> |
.CS code
|
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class DynamicPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["MyTitleId"] != null) { string strId = Request.QueryString["MyTitleId"].ToString(); DisplayArticle(strId); } }
private void DisplayArticle(string strId) { string strConn = ("Data Source=.; uid=sa; pwd=Password$2; database=userinfo"); SqlConnection conn = new SqlConnection(strConn); SqlDataAdapter da = new SqlDataAdapter("select Id,Title,Description,author from Articletable where Id=" + strId, conn); DataSet ds = new DataSet(); da.Fill(ds, "Articletable"); lblTitle.Text = ds.Tables["Articletable"].Rows[0][1].ToString(); lblDescription.Text = ds.Tables["Articletable"].Rows[0][2].ToString(); lblauthor.Text = ds.Tables["Articletable"].Rows[0][3].ToString(); } protected void Button1_Click(object sender, EventArgs e) {
} }
|
URLRewriter.dll
Now Add the reference of the URLRewriter.dll in the bin folder of the application witch is attached with the download file.Add a image
Add Image in the images folder of the application that will be display with the title of dynamic page at run time witch is also attached with the download file.
Now run the application and test it.

Figure3
Now click on the title link. to redirect the dynamic page.

Figure4
Conclusion:
The URL Rewriting is very useful for working with community website. If there is any mistake in this article then please notify me. I expect your valuable comments and feedback about this article.

Lokesh RavindraPosted Jun 10, 2015, 9:02 AM
In local it is working fine.. but in server I got HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. how to fix it ?
sachindra singhPosted Jun 8, 2015, 7:35 AM
Thanks i Get solution its my mistake thanks
sachindra singhPosted Jun 8, 2015, 7:22 AM
i got HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. error
sachindra singhPosted Jun 8, 2015, 7:21 AM
sir when make a copy of both page gridview and dynamicpage and remane and in web.congif create a <rewrite url="~/sachin/(.+)-(.+).aspx" to="~/myfiles.aspx?MyTitleId=$2"/> and in also change strTitle = "~/sachin/" + strTitle + "-" + strId + ".aspx"; the not working
Lokesh RavindraPosted Jun 6, 2015, 7:38 AM
I'm getting cannot use a leading .. to exit above the top directory error while applying same code to my application. how to fix it ?
Firoz KhanPosted May 8, 2014, 8:58 AM
thanks man, great
Aman MiglaniPosted Jan 21, 2014, 12:25 AM
please check the url when you host your site on server and do re-writeing according to your URL which is showing when it is upload on server Thank you
abc abceditedPosted Apr 22, 2013, 5:38 PMEdited Apr 22, 2013, 5:42 PM
Help me! How to get catergory_id from url: domain.com/catergory-name/product-name.html to show other products same catergory_id?
abc abcPosted Apr 10, 2013, 12:02 PM
Thank you very much!
Guest UserPosted Oct 25, 2012, 3:43 AM
goood idea. specially GenerateUrl method.
P MeditedPosted Oct 23, 2012, 12:08 PMEdited Oct 23, 2012, 12:09 PM
works fine for me locally... not sure about on the server
samidha dokePosted Jul 14, 2012, 1:41 AM
this is not working.. am getting error as "resource can not be found" ..
Jayesh GorePosted May 16, 2012, 5:55 AM
your url rewriting example is not working fine on server. it show error " the resource cannot be found " but working fine in localhost but on server in show error " the resource cannot be found " plz give me the solution.
AntonioeditedPosted Feb 28, 2012, 7:04 AMEdited Feb 28, 2012, 7:05 AM
How can I use the function on multiple pages without having to rewrite the function at all? Thank you, very good article.
irfan irfanPosted Dec 9, 2011, 6:19 AM
your url rewriting example is not working fine on server. it show error " the resource cannot be found " but working fine in localhost but on server in show error " the resource cannot be found " plz give me the solution.