Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1


Ratings: (0)|Views: 6|Likes:
Published by Samkit Shah

More info:

Published by: Samkit Shah on Aug 17, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less





Top of Form
I hope you hate URLs that look like this www.mysite.com/page.aspx?something=1&somethingelse=15&other=abcd&category=32. If you do, let's have a littlereminder on
rewriting URLs. But, if you don't, please take this article seriously :)
So, you hate such URLs
Or you'll hate it after you realize how easy it is to make it nicer. Besides all, URL rewritingwill improve your rankings on search engines. Search engines like Google will easily indexyour "static" URLs, instead of dynamic URLs. There are several ways to accomplish URL Rewriting. I'll explain how to do this by usingHttpModule and how to overcome the postback bug that is the outcome of URL rewriting.
Suppose you have a content management system that stores entire pages in the database.So you can have a Home page, and Home page can have sections Products and Services,and each one of these can have their own child pages or sections, and so on.So we want dynamic URL like this: Default.aspx?SectionID=5&ItemID=22 to look like/catalogue/furniture/chairs/chair5.aspx or whatever the business logic requirement is.In the example in this article I will not use a database in order to keep it simple, but youimagine there is a database that keeps the URL for each page. I'll use hard-coded Dictionarythat will keep some sample pages.Note: You can download the full code in the attachment.First, we'll make a data access object that will search the database for requested url andreturn its dynamic url. These are the methods in SampleDAO that will simulate the databaseand getting the url from there:
public string GetRealPath(string requestedUrl){string path = "";Dictionary<string, string> paths = GetPathsFromDatabase();if (paths.ContainsKey(requestedUrl))paths.TryGetValue(requestedUrl, out path);return path;}private static Dictionary<string, string> GetPathsFromDatabase(){Dictionary<string, string> paths = new Dictionary<string, string>();paths.Add("/URLRewrite/FirstSection/Default.aspx".ToLower(), "/URLRewrite/Default.aspx?SectionID=1");paths.Add("/URLRewrite/SecondSection/Default.aspx".ToLower(),"/URLRewrite/Default.aspx?SectionID=2");paths.Add("/URLRewrite/FirstSection/Page1.aspx".ToLower(), "/URLRewrite/Default.aspx?SectionID=1&Item=1");paths.Add("/URLRewrite/FirstSection/Page2.aspx".ToLower(), "/URLRewrite/Default.aspx?SectionID=1&Item=2");
paths.Add("/URLRewrite/SecondSection/Page1.aspx".ToLower(), "/URLRewrite/Default.aspx?SectionID=2&Item=1");paths.Add("/URLRewrite/SecondSection/SubSection/AnotherOne/Page5.aspx".ToLower(),"/URLRewrite/Default.aspx?SectionID=2&Item=5");paths.Add("/URLRewrite/Default.aspx".ToLower(), "/URLRewrite/Default.aspx");return paths;}
What does this means? When you create links on your web pages, you will no longer usequery parametrised URLs but rather static URLs. Now what? If you request a page thatdoesn't exist, HttpException will be thrown or default redirection will occur, but this is theplace where Rewrite module comes in.Rewrite module (in our Example UrlRewriteModule) implements IHttpModule interface. Thismeans you will have to implement two methods: Init and Dispose. Init method is moreimportant. It accepts HttpApplication and lets you hook on whatever you want. In our casewe'll hook on Application BeginRequest event so that we can read a requested URL eachtime a new request begins its lifecycle.Look at the code below. In the Application_BeginRequest event handler we get therequested url which is a static URL, and search the database to find the correspondingdynamic url.
public void Init(System.Web.HttpApplication app){app.BeginRequest += new EventHandler(Application_BeginRequest);}
private void Application_BeginRequest(object sender, EventArgs e){System.Web.HttpApplication app = (System.Web.HttpApplication)sender;string requestedUrl = app.Request.Path.ToLower();string realUrl = GetRealUrl(requestedUrl);
if (!String.IsNullOrEmpty(realUrl))app.Context.RewritePath(realUrl, false);}

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->