URL REWRITING/VIRTUAL URL

Kasarlaravi
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 19768 red flag
Rating: 5 out of 5  
 1 vote(s)

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//…


Page copy protected against web site content infringement by Copyscape

About the Author

Kasarlaravi
Full Name: ravi kasarla
Member Level: Starter
Member Status: Member
Member Since: 6/30/2011 7:08:03 AM
Country: India
Ravi Kasarla
http://www.dotnetfunda.com
HI , i am currently working for oztek software pvt ltd as a web developer.i am having 1.5 years experience on .net frame work and php.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)