ASPHostCentral.com ASP.NET MVC Hosting BLOG
All about ASP.NET MVC 4.0 Hosting, ASP.NET MVC 3.0 Hosting and ASP.NET MVC Hosting articles
|
ASPHostCentral.com :: Hosting and Deployment of an ASP.NET MVC Application in IIS6 and IIS7 >>
ASPHostCentral.com :: Using ASP.NET MVC Routing to route the 404 Error Pages
March 19, 2009 21:51 by
Administrator
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.
Where to go for ASP.NET MVC Hosting?
You can host your website on the ASP.NET MVC Framework at
ASPHostCentral.com
and you can do it by just paying
$4.99/month
Currently rated 3.0 by 12 people
Currently 3/5 Stars.
1
2
3
4
5
Related posts
ASP.NET MVC 3 Hosting :: Error Handling and CustomErrors in ASP.NET MVC 3 Framework
So, what else is new in MVC 3? MVC 3 now has a GlobalFilterCollection that is automatically pop...
ASPHostCentral.com :: Hosting and Deployment of an ASP.NET MVC Application in IIS6 and IIS7
Hosting and Deployment of an ASP.NET MVC Application in IIS6 and IIS7
ASP.NET MVC Hosting :: An Architectural View of the ASP.NET MVC Framework
An Architectural View of the ASP.NET MVC Framework
ASP.NET MVC Hosting
ASPHostCentral is a premier web hosting company where you will find low cost and reliable web hosting. We have supported the latest
ASP.NET 4.5 hosting
and
ASP.NET MVC 4 hosting
. We have supported the latest
SQL Server 2012 Hosting
and
Windows Server 2012 Hosting
too!
Search
Include comments in search
Tag cloud
.net 4.5 hosting
.net mvc 3 hosting
.net mvc 4 hosting
404 error page
asp.net 4.5 hosting
asp.net mvc
asp.net mvc 2 rc hosting
asp.net mvc 3 hosting
asp.net mvc 3.0 hosting
asp.net mvc 4 hosting
asp.net mvc 4 hosting .net mvc 4.0 hosting
asp.net mvc 4.0 hosting
asp.net mvc hosting
asphostcentral
asphostcentral.com
cheap .net mvc 3 hosting
cheap .net mvc 4 hosting
cheap asp.net mvc 3 hosting
cheap asp.net mvc 4 hosting
cheap hosted asp.net mvc 3
cheap hosted asp.net mvc 4
hosted asp.net mvc 3
hosted asp.net mvc 4
iis7
mvc 3 hosting
mvc 3.0 hosting
mvc 4 hosting
mvc 4.0 hosting
mvc framework
mvc framework hosting
mvc hosting
mvc routing
webmatrix hosting
windows 2012 hosting
Month List
2013
April (1)
2012
October (1)
September (4)
August (1)
March (5)
2011
November (5)
October (3)
September (3)
August (2)
July (1)
June (1)
May (3)
April (1)
March (8)
January (2)
2010
December (1)
November (2)
September (1)
April (4)
February (1)
January (1)
2009
October (1)
July (1)
April (2)
March (3)
Other Hosting BLOG
ASPHostCentral.com
ASP.NET MVC Hosting BLOG
Windows 2008 Hosting BLOG
General Hosting BLOG
ASP.NET Hosting BLOG
Sharepoint Hosting BLOG
Crystal Report Hosting BLOG
SSRS Hosting
Sharepoint 2010 Hosting BLOG
.NET4 Hosting BLOG
European Windows Hosting
ASPHostCentral.com REVIEW
ASPHostCentral.com Twitter
Sharepoint Hosting NEWS
SQL 2008 R2 Hosting News
Silverlight WCF RIA Service Hosting News
ASP.NET 4.0 Hosting News
Christian BLOG
Sign in