|
|
|
|
|
|
|
Page Views :
|
3082
|
|
Downloads :
|
0
|
|
Rating :
|
Rate it
|
|
Level :
|
Advanced
|
|
|
Introduction
This article describes a way to use ASP.NET Routing to avoid 404 Not Found errors when changing folder structure or folder names in a web site.
What to do with obsolete links to your web site?
Having a web site means spending some time and efforts promoting the site on the Internet, making sure search engines indexed all the pages, trying to get exposure through blogs or discussion boards.
And then you get new idea and really need to restructure your site "change some folder names, move some pages, etc. What will happen with all those third-party links to your site you were so proud of? Do you want to lose them?
Route old URLs to new site structure
With arrival of .NET Framework 3.5 SP1 we got an elegant way of solving this problem ASP.NET Routing. Initially it was a part of ASP.NET MVC Preview 2, and now it is a part of the Framework.
The idea is to add special "Routs" to the site having single goal of processing requests to pages which are no longer present on the site. In its simplistic form the processing can happen in a single ASPX page responsible for proper handling of requests. Here is the example.
These parts you'll need in the project:
WebFormRouteHandler created by Chris Cavanagh representing IRouteHandler implementation, Global.asax file registering your Routs, web.config file where you register the WebFormRouteHandler , and Default.aspx page responsible for actual request processing.
Let's take a look at Global.asax.
void Application_Start(object sender, EventArgs e) { // runs on application startup RegisterMyRoutes(System.Web.Routing.RouteTable.Routes); }
private void RegisterMyRoutes(System.Web.Routing.RouteCollection routes) {
// reference IRouteHandler implementation (example created by Chris Cavanagh) // see http://chriscavanagh.wordpress.com/2008/03/11/aspnet-routing-goodbye-url-rewriting/ var startPageRouteHandler = new WebFormRouteHandler("~/default.aspx");
// exclude .axd to handle web services and AJAX without checking all routs // see http://msdn.microsoft.com/en-us/library/system.web.routing.stoproutinghandler.aspx routes.Add(new System.Web.Routing.Route("{resource}.axd/{*pathInfo}", new System.Web.Routing.StopRoutingHandler())); routes.Add(new System.Web.Routing.Route("{service}.asmx/{*path}", new System.Web.Routing.StopRoutingHandler())); // mapping: // extracts folder name and page name as items in HttpContext.Items routes.Add(new System.Web.Routing.Route("{folderName}/", startPageRouteHandler)); routes.Add(new System.Web.Routing.Route("{folderName}/{pageName}", startPageRouteHandler ); }
Here we defined single route handler - default.aspx, as well as routing rules.
Rule #1:
routes.Add(new System.Web.Routing.Route("{folderName}/", startPageRouteHandler));
states that all requests to a URL with structure "http://mysite.com/something" will be processed by the default.aspx page if there was no actual "something" found on the site. For example, there is a RealPage.aspx page present on the site, so requests to http://mysite.com/RealPage.aspx will be processed by that page.
But if client requests RealPage2.aspx, that request will be processed by the default.aspx page according to the rule #1. Note that client will not be redirected to default.aspx, it will be just web server running code in default.aspx in response to the request. For the client the response will come from the RealPage2.aspx.
You can add as many routes as you want, for example rule #2:
routes.Add(new System.Web.Routing.Route("{folderName}/{pageName}", startPageRouteHandler));
stating that all requests to a URL with structure "http://mysite.com/somefolder/somethingelse" will be processed by the default.aspx page if there was no actual "somefolder/somethingelse" found on the site.
The code behind default.aspx shows how to extract those parts of the request. As you can see they will be placed in the HttpContext.Items collection.
lblFolder.Text = Context.Items["folderName"] as string; lblPage.Text = Context.Items["pageName"] as string;
How it works in real life
Here is a real life web site actually using this technique - Digitsy Global Store. Besides handling obsolete URLs the ASP.NET Routing is being used to handle multiple languages on the site, switching CultureInfo on the fly:
protected void Page_PreInit(object sender, EventArgs e) { CultureInfo lang = new CultureInfo(getCurrentLanguage()); Thread.CurrentThread.CurrentCulture = lang; Thread.CurrentThread.CurrentUICulture = lang; } private static string getCurrentLanguage() { string lang = HttpContext.Current.Items["language"] as string; switch (lang) { case "france": return "fr-FR"; case "canada": return "en-CA"; case "germany": return "de-DE"; case "japan": return "ja-JP"; case "uk": return "en-GB"; case "russia": return "ru-RU"; default: return "en-US"; } }
As you see, the default language is English, United States: "en-US". In internal links the site uses structure http://{sitename}/{language}/¦other things¦
So if you try http://digitsy.com/us/ you'll get US version, trying http://digitsy.com/japan/ will bring you Japanese one, and if you try http://digitsy.com/whatever " you'll not get 404 error, you'll get US version again.
ASP.NET Routing made restructuring of the site really easy. The folder structure "{language}/{index}/category/{categoryID}" was recently replaced by "{language}/{index}/shopping/{categoryID}". There supposed to be no "category" folder in the site structure anymore. But because of both routs pointing to the same handling page both folders "category" and "shopping" return valid responses.
Trying http://digitsy.com/us/Electronics/shopping/541966 will use the rule:
routes.Add(new System.Web.Routing.Route("{language}/{index}/shopping/{categoryID}", categoryRouteHandler));
while trying http://digitsy.com/us/Electronics/category/541966 will use:
routes.Add(new System.Web.Routing.Route("{language}/{index}/category/{categoryID}", categoryRouteHandler));
and both will resolve to the same route handling page.
Things to remember
This is really simple if you know what you are doing. I mean, you should be aware of some implications. Check out Phil Haack's post discussing "one subtle potential security issue to be aware of when using routing with URL Authorization."
You should also verify if your hosting provider supports SP1 for .NET Framework 3.5. Many of hosting providers still don't have SP1 installed on their servers because of incompatibility with some old software.
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|