Click here to Skip to main content
13,248,927 members (45,924 online)
Click here to Skip to main content
Add your own
alternative version

Stats

23.3K views
17 bookmarked
Posted 20 Mar 2007

Url rewritting in some simple steps...(My simple way series Article)

, 20 Mar 2007
Rate this:
Please Sign up or sign in to vote.
Dont worry Url rewritting is simple and easy...

Introduction

Sometimes, we want that our web url will not contain any "?","&" or other kind of non readable symbols, but the question is that how to remove them from the browser's address bar, but that information is very essential for us because they are query parameters for us. So it is like we are hiding information of query string from the user by displaying some nice and readable contains, but with at server side we want our query parameters back. So that is called "URL rewriting". Don't worry it is not a difficult task.

Background

Before that, we have to have some knowledge of how the requests are processed on ASP.NET server? Remember that when any request is coming it has to pass through "Application_BeginRequest()" method in Global.asax file.

This is that access point of each and every requests those are coming to our application.So cheers it is that point where we have to work for URL rewriting.

Using the code

1. First we have to add "Global.asax" file into our application.

// Write this method prototype like this,
void Application_BeginRequest(object sender, EventArgs e)
// write structure of code like this... 
// First get the url of the request,

string absoluteUrl = Request.Url.AbsolutePath.ToString();
//then search for the specific page into that url of request,(means you can 
//rewrite the url for some speific pages only. 

if (absoluteUrl.Contains("/test/default.aspx"))
{ 
//now search for some redable query string parameter like you have embedded
//url link like this...http://test.com/test/default.aspx?mypageid=33 

//here instade of this we can write like..  
//http://test.com/test/default.aspx/MyPage33 
//so you can see we have removed unreadable and ugly codes from url.
// now how to handle this and converting this back to our real information.

//find the location of last "/" character because from that "MyPage33"
//word is starting. 

string pageid = absoluteUrl.Substring
            (absoluteUrl.LastIndexOf('/') + 1).Trim();
//now skip first 5 characters because they are "MyPage".
pageid = pageid.Substring(6); //this will give "33".
// now generate the original Url which is required by our application.   

string path = "~/test/default.aspx?mypageid=" + pageid;                    
//now redirect to the target page, 

Response.Redirect(path);
//Bengo!!!! 
//We have successfully done with Url rewriting.
So it is really a simplest way to rewrite the URL isn't it?

Points of Interest

I like to Share my knowledge with guys like you, because i am also one of you, that if i dont know anything then i come to you...so keep exchange of knowledge...

History

keep attached with my simple way series articles....

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

JustChiragPatel
Software Developer
India India
Chirag Patel, a Programmer Analyst in a well known IT company working on .NET Technologies since last 2 years. He is interested in Pure business logic and fuzzy logic. his area of interest is in C#.NET, VB.NET and MSSQL 2005.

catch me on: http://groups.google.com/group/codewar

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
aphazel16-Nov-09 1:31
memberaphazel16-Nov-09 1:31 
GeneralMy vote of 1 Pin
BooGhost22-Apr-09 20:36
memberBooGhost22-Apr-09 20:36 
GeneralMy vote of 1 Pin
Adam R Oldfield2-Dec-08 16:11
memberAdam R Oldfield2-Dec-08 16:11 
GeneralFor Simplest Way of writing URL Rewriting Pin
DotNetGuts24-Jul-08 11:40
memberDotNetGuts24-Jul-08 11:40 
GeneralThis is absolutelly wrong. Pin
Gevorg22-Mar-07 10:35
memberGevorg22-Mar-07 10:35 
GeneralBrowser Refresh Pin
SiPurdy21-Mar-07 1:35
memberSiPurdy21-Mar-07 1:35 
GeneralRe: Browser Refresh Pin
Gevorg22-Mar-07 10:38
memberGevorg22-Mar-07 10:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 21 Mar 2007
Article Copyright 2007 by JustChiragPatel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid