Let us assume that ,we need to display the user public profile information in viewprofile.aspx page
Generally we pass the user id in the qurey string like ”viewprofile.aspx?id=100” . We fetch data using id value and displays the information .
If client request s you to url format to be like http://yoursite.com/ publicprofile /username
For that type of cases we use the url rewriting concepts .
Introduction
In this article we will learn url rewriting ,virtual url concepts
URL rewriting is the process of accepting an incoming Web
request and automatically redirecting it to a different URL.
Using the code
Create “publicprofile” directory in your web application.
Global.asax
void
Application_BeginRequest(object sender, EventArgs e)
{
string
CurrentPath = Request.Url.ToString();
if
(CurrentPath.Contains("/publicprofile/"))
{
HttpContext
MyContext = HttpContext.Current;
string
url = CurrentPath.Substring(0,CurrentPath.LastIndexOf("/"));
string
username = CurrentPath.Substring(CurrentPath.LastIndexOf("/"));
username = username.Replace("/", "");
if
(CurrentPath[CurrentPath.Length - 1].ToString() == "/")
{
HttpContext.Current.Response.Status
= "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location",
Request.Url.ToString().Replace(CurrentPath, url));
}
else
{
MyContext.RewritePath("index.aspx?uname=" + username);
}
}
}
Place “Index.aspx” in “
publicprofile”
directory.
Index.aspx.cs
protected void Page_Load(object
sender, EventArgs e)
{
if
(Request.QueryString["uname"] != "")
{
int id = Validateuser.getId(Request.QueryString["uname"].ToString());
if
(id != 0)
{
ProfileInfo(id);
}
else
{
Response.Redirect(“http://yoursite.com/?err=1");
}
}
else
{
Response.Redirect(“http://yoursite.com/?err=1");
}
}
protected void ProfileInfo(int
proID){
DataTable dt = Validateuser.getprofile(proID);
//Code to assign values to controls……
}
Web.config:
<httpModules>
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing,
Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</httpModules>
Conclusion
The above code works for the following urls http://yoursite.com/ publicprofile /username,
publicprofile /username/,publicprofile /username//…