Geeks With Blogs

News Awarded Microsoft MVP C#.NET - 2007, 2008 and 2009


I am born in Bangladesh and currently live in Melbourne, Australia. I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net)and born in Bangladesh.
I am founder and Chief Executive Officer of
Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Co-founder and core developer of Pageflakes www.pageflakes.com.
Simplexhub, is on its mission to build a smart virtual community in Bangladesh and recently launched beta realestatebazaar.com.bd an ASP.NET MVC application written in C#.NET.


Some of My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Shahed Khan blog

Routing Library resides in the System.Web.Routing Namespace of the .NET Framework 3.5, which provides us the flexibility to use URLs that has no mapping to a physical file. This means ASP.NET MVC framework provides flexible URL mapping engine and enables us to write SEO (Search Engine Optimization) friendly URLs with very little effort. No one can deny the importance of SEO, to be successful in search based marketing. What better way to analyze a business than from what customers are looking for on the Internet through keyword research. SEO is the way to go.

SEO friendly URL Format for an e-commerce application may be
/Products/List/ProductCategory   
/Products/Detail/ProductName

URL Example
/Products/List/CareCare
/Products/Detail/MiracleCarDuster

In ASP.NET MVC world the URL Routing System maps the incoming URLs to the relevant Controller and Action, in the above example our Contoller is Products and Action is List or Detail. We normally go and define a Route object and add it to the RouteCollection and register the RouteCollection during Application_Start(). Out of the box System.Web.Mvc library ships with the RouteCollectionExtensions which allows us to define routes easily using the the different overloads of MapRoute method.

Example:

public static void RegisterRoutes(RouteCollection routes)
{
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");             

           routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

}

If you look under the hood you will find, a Route object is created and added to the RouteCollection.

Route route = new Route(url, new MvcRouteHandler()){};
.....
.....
routes.Add(name, route);

note that "MvcRouteHandler" have been passed as a parameter by default, when we use the routes.MapRoute() method. The overloads of MapRoute() extension methods are just helper methods to make things easy for us, it is not mandatory that we will have to always use this. We can define a Route object in plain .NET code and assign necessary properties to it, we can also pass our preferred IRouteHandler. This gives superb flexibility with handling URLs. For instance In a practical world of web marketing / search marketing we always need to support Legacy URLs. In the web marketing world ad hoc campaigns are launched, and what may have worked last week may not work this week any more, as a result the URL structure changes frequently and we face the need to start redirecting to the new URLs.

On Top of that during this transformation of URLs we need to implement "301 Moved Permanently" redirections, which means the previous URL has been permanently removed and all future requests should be directed to the given new URI. Handling this scenario has become very easy with the Routing Engine. All we need to do is add a new Route or modify the existing Route to fit our need. Matt Hawley has an excellent post on Legacy Url Routing which describes how to route existing aspx file based URLs to the appropriate MVC Controller and Action. This article also gives directions on how to implement custom Route and custom RouteHandler.

public static void RegisterRoutes(RouteCollection routes)
{
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");             

           routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

  routes.Add("", new LegacyRoute(
    "Users/Login.aspx",
    "Login",
    new LegacyRouteHandler())); // Defined a Custom Route class and have passed a custom IRouteHandler.

}



As you can see in the above code, how easily we can add a custom route and pass a custom IRouteHandler (in this case LegacyHandler).

Tracking - is another common task performed, we want to track everything, every single clicks a consumer performs on the site. This has also becomes easy in the world of ASP.NET MVC. By design we define separate actions for each functionality and we Route to the Controller - Action to get anything done. So tracking would be a viable option to do centrally just before creating the Controller object or just before delegating to the Action.

You will notice the implementation of IRouteHandler requires to implement only one method "GetHttpHandler" which returns a IHttpHandler

public interface IRouteHandler
{
        IHttpHandler GetHttpHandler(RequestContext requestContext);
}

You will find the MvcRouteHandler implements IRouteHandler like this

public class MvcRouteHandler : IRouteHandler {
        protected virtual IHttpHandler GetHttpHandler(RequestContext requestContext) {
            return new MvcHandler(requestContext);
        }

}

The real delegation of a Route to a Controller and Action happens in the ProcessRequest(HttpContext context); method of the MVCHandler class which is the implementation of IHttpHandler. The ProcessRequest(HttpContext contxt) method may be a be a good place to centrally control tracking in one of our custom Handlers. The implementaiton of MVCHandler is as follows, where you can see how a controller is created by the IControllerFactory factory, and then Execute() method is called. We can do our tracking somewhere before calling the Execute() method.

protected internal virtual void ProcessRequest(HttpContextBase httpContext) {
            AddVersionHeader(httpContext);

            // Get the controller type
            string controllerName = RequestContext.RouteData.GetRequiredString("controller");

            // Instantiate the controller and call Execute
            IControllerFactory factory = ControllerBuilder.GetControllerFactory();
            IController controller = factory.CreateController(RequestContext, controllerName);
            if (controller == null) {
                throw new InvalidOperationException(
                    String.Format(
                        CultureInfo.CurrentUICulture,
                        MvcResources.ControllerBuilder_FactoryReturnedNull,
                        factory.GetType(),
                        controllerName));
            }
            try {
                controller.Execute(RequestContext);
            }
            finally {
                factory.ReleaseController(controller);
            }
        }


Did you know, Routes in the ASP.NET Mvc are matched and executed on a first match bases! It may be important to order the routes so that the correct pattern is matched first before a more general pattern matches and executes it. It is sometimes hard to figure out which particular pattern will be caught first when we have a lot of routes, ASP.NET Routing Debugger comes in to rescue, Phil Hack has put together this nice little route tester utility which can save a lot of time. This utility quickly displays in Red and Green color what Route patterns have matched for a particular URL. So we can type in various URLs in the addressbar to see which routes matches.

Lets now looks at a different problem, we normally define all the routes in the global.aspx.cs file, this causes a problem when Routes changes frequently, every single time a new route is added or an existing one is modified we need to recompile web application and upload the new dll to the server, again it is not mandatory to write Routing rules in the global.aspx.cs file, we can easily store the routing rules to a Xml file and use a combination XML related .NET libraries and .NET Reflection APIs to read from the Xml file and create/deserialize Route Objects to add them to the RouteCollection during the Application_Start(). But still we haven't overcome the limitations of restarting the application as the RouteCollection gettting registered during Application_Start. I think we have to live with that, unless we go and implement some kind of  FileSystemWatcher to monitor the Xml file and force to refresh the RouteTable.Routes object when the xml file changes. I haven't tried implementing this yet but this would work I think.

We have discussed here, how ASP.NET Routing engine eases writing SEO friendly Url, maitaining Url redirections and tracking centrally. Hope this helps.

Thank you for being with me so far.

Posted on Saturday, February 14, 2009 6:06 AM | Back to top


Comments on this post: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
I was just thinking about SEO and you’ve really helped out. Thanks!
Left by David Ascot on Mar 18, 2009 9:49 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
I will definitely pass this along to our developers. I'm not very well schooled in programming languages, but this really does look like a good way to do this.
Left by Bronson on Apr 09, 2009 12:22 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Wow, I never knew that Routing Engine to aid SEO / 301 Redirect / Tracking. That’s pretty interesting...
Left by Internet Marketing Company on Jun 12, 2009 5:46 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
This is really amazing post ...this is very helpful information.
Left by Internet Marketing Company on Jul 27, 2009 2:29 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Saved my day. And i have passed it to my fellow developers.
Left by sem on Jul 30, 2009 9:49 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
goooooooooooooood thanks
Left by توبيكات on Aug 12, 2009 8:55 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
thanks for this topic see online
Left by مركز تحميل on Aug 12, 2009 8:56 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
The wonders of ASP. Net are simply make things great.Jobs in Dubai | Finance Jobs in Dubai
Left by Dubai Jobs on Aug 13, 2009 10:08 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
I never knew that Routing Engine to aid SEO,this is very helpful information. Thanks for giving this valuable information
Left by artificial grass turf on Aug 21, 2009 5:02 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Thanks a lot for this info. It's so useful for me.
Left by Belajar SEO on Aug 24, 2009 3:11 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Thanks for the information.

One question: how to handle 301 redirects for directing: http://altafkhatri.com/ to http://www.altafkhatri.com/

Left by Altaf on Aug 24, 2009 6:15 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Hi,

I never knew that Routing Engine to aid SEO / 301 Redirect / Tracking. That’s pretty interesting.
I thought this is same like canonical url's

Thanks.
Left by Roulette on line on Aug 24, 2009 9:35 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Dear altaf

You asked: how to handle 301 redirects for directing: http://altafkhatri.com/ to http://www.altafkhatri.com/

Answer: Please refer to my blog post: ASP.NET MVC Tips: 301 Redirect non-www versions of URL to www.

http://geekswithblogs.net/shahed/archive/2009/05/19/132204.aspx

I hope this helps.
Left by Shahed Khan on Aug 24, 2009 11:08 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Great discussion thanks for this topic see online ....
Left by internet marketing on Aug 25, 2009 3:41 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
I thought this is same like canonical url's

Thanks.
Left by افلام on Aug 26, 2009 3:22 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Thank you and the subject site's outstanding
Left by منتدياØ& on Aug 26, 2009 3:49 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
I am planning to design a website and hope i will need the help of this article.

thanks
Left by Link Building Services on Aug 27, 2009 4:03 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Great information. Thanks for share..
Left by internet marketing on Aug 28, 2009 9:39 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Any chance you could create a dll for us that would do the 301 redirect if someone comes in without a www ? I imagine that would save a lot of duplication for your readers
Left by Deals on Aug 30, 2009 2:55 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
I'm not very well schooled in programming languages, but this really does look like a good way to do this.
Left by chocolates on Aug 31, 2009 4:08 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Thank you and the subject site's outstanding
Left by Nainital Hotels on Aug 31, 2009 4:09 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Nice info, we are doing real estate website designing & this post will definitely helps me.




Left by Real Estate Website Design on Aug 31, 2009 5:45 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Why people are very focus on SEO. Kindly inform me some future strategies regarding SEO. Thanks admin
hot car picture
adadadadad
tatil
Left by sesli sohbet on Sep 03, 2009 10:00 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Thanks admin nice post
adadadad
tekne
adadada
barbie oyunu
Left by manken on Sep 03, 2009 10:01 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Thank you and the subject site's outstanding
Thanks admin nice post
diyet
dadada
manken resimleri
Left by su pompası on Sep 03, 2009 10:04 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
thanks for the nice tips. Routing engine for seo sounds good to me.
Left by SEO Dubai on Sep 06, 2009 6:09 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
I have sound experience in asp.net MVC structure but this was something different and excite me to use this tips practically. i used to do URL rewriting through web.config file..
Left by asp.net web development on Sep 07, 2009 8:49 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Nice info, we are doing real estate website designing & this post will definitely helps me.
Left by العاب طبخ on Sep 08, 2009 4:16 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
thanks admin for article
Left by sesli chat on Sep 11, 2009 4:52 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Its always good to learn tips like you share for blog posting. As I just started posting comments for blog and facing problem of lots of rejections. I think your suggestion would be helpful for me. I will let you know if its work for me too.
Left by ام البنات on Sep 13, 2009 4:54 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
ASP.Net language is great for SEO.
Left by Dubai for rent on Sep 14, 2009 4:41 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
good thanks yuo for the nice tips. Routing engine for seo sounds good to me.
Left by منتديات الغالي on Sep 17, 2009 6:22 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
good to learn tips like you share for blog posting. As I just started posting
Left by صور مسنجر on Sep 17, 2009 6:28 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Thank you and the subject good
Left by العاب كمبيوتر on Sep 17, 2009 6:29 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Great to se your post. I will use it for future referencing.
Left by SEM Dubai on Sep 17, 2009 6:59 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
subject good
Left by العاب on Sep 18, 2009 2:23 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
ure referencing.
Left by العاب بنات on Sep 18, 2009 2:24 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Engine to aid SEO /
Left by العاب طبخ on Sep 18, 2009 2:26 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
ngine to aid SEO /
Left by العاب تلبيس on Sep 18, 2009 2:26 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
or future referencing.
Left by دليل on Sep 18, 2009 2:28 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
I was just thinking about ASP.NET MVC tips and you've really helped out. Thanks!
Left by enterprise mobility solution on Sep 26, 2009 9:18 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Thanks for sharing these tips mate.
Left by IT Consultants Dubai on Sep 27, 2009 11:14 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
I already made a concious decision about why it's fine the way it is. I understand that design is subjective, but communicating to a client that they can trust our expertise is very difficult.
Left by Motorbike club on Sep 28, 2009 12:20 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
thanks for sharing
Left by muscle building supplements on Sep 28, 2009 1:11 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
this information is a good share.
Left by Information Tecnology UAE on Sep 28, 2009 5:19 PM

# web design India,outsource web development India,wai,W3C design
Requesting Gravatar...
*web design India*,outsource web development india,emarketing company india,design W3C,WAI web designer ahmedabad
Left by Jitendra Ravia on Sep 29, 2009 5:43 PM

# brahm samaj,brahmin samaj,brahmin community,brahminism
Requesting Gravatar...
brahm samaj have a mission to unite all Brahmin community to work for Brahminism
Left by Jitendra Ravia on Sep 29, 2009 5:45 PM

# web hosting India Ahmedabad,Surat,Baroda,Rajkot,India,UK,Canada
Requesting Gravatar...
*web hosting india*,Surat,Baroda,Rajkot Offers reseller hosting
India,shared hosting India,dedicated server India,windows
hosting,linux hosting,web hosting Ahmedabad
Left by Jitendra Ravia on Sep 29, 2009 5:47 PM

# gujarati news paper online
Requesting Gravatar...
*gujarati news paper online*,*gujarati news paper bhavnagar* ,*gujarati news paper rajkot*,*gujarati news paper ahmedabad*,*gujarati news paper jamnagar*,*gujarati news paper surat*
Left by Jitendra Ravia on Sep 29, 2009 5:48 PM

# good work
Requesting Gravatar...
Shahid where you base. I f we need to cntact you how can we make it?
Left by Web Designers Dubai on Sep 30, 2009 6:52 PM

# article
Requesting Gravatar...
Thanks for the article.
Left by medical tourism on Oct 02, 2009 2:53 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Thanks a lot for this info. It's very useful for me.
Left by Extra Money online on Oct 05, 2009 4:30 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
thanks for the nice tips. Routing engine for seo sounds good to me. I never knew that Routing Engine to aid SEO / 301 Redirect / Tracking. That’s pretty interesting.
Left by profitable franchise business op on Oct 06, 2009 4:40 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
Rodrigo, you don’t have to write tests, it’s up to you. It’s an opportunity, and not a cons. Classic ASP is different from MVC Views. Views are just presentation, it’s not necessary that View should generate HTML code, as mentioned, it can be RSS or a mobile application code. So I would consider it also as a great opportunity.
Left by London Escorts on Oct 10, 2009 4:40 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
The prompt control builds upon the ASP.NET AJAX framework and derives from the “ScriptControl” server-side class and the “Sys.UI.Control” client-side class. The prompt control also utilizes extender controls with client-side behaviors.
Left by folding poker table on Oct 13, 2009 4:28 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
During the past few months I had stated that explicitly declaring these dependencies makes construction of the object more complex in my lecture at Para legal school.
Regards,
Left by membuat blog on Oct 19, 2009 9:06 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
thanks for this topic
Left by يوتيوب on Oct 25, 2009 11:43 PM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
thansk you for sites.
Left by porno izle on Oct 26, 2009 4:13 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
hello sites thansk you topic.
Left by sikiş izle on Oct 26, 2009 4:14 AM

# re: ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking
Requesting Gravatar...
I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.
Left by objek wisata di pandeglang on Oct 26, 2009 11:45 PM

Your comment:
 (will show your gravatar)


Copyright © Shahed Khan | Powered by: GeeksWithBlogs.net