URL Rewriting In ASP.NET

Introduction

While developing a website, you may need to hide the extensions of your web pages due to many reasons such as;
  •  You may not need to show the technology you are using to the internet users.
  •  You might need to mask the url name with some other appropriate names.
  •  For better SEO, I mean search engine friendly URL.
  •  For more User friendly URL.
So, here I am going to explain you the simplest way to mask your url according to your need and wish in ASP.NET.



Behind The Scene

In your solution explorer ( in visual studio ) add a folder and name it Technology ( you name it according to your choice ) and now put the webpages you want to be there inside the newly created folder.

** mind it only .aspx pages can be masked using this technique no .html page can be.

Now add a .cs ( class file ) to the root folder of your solution. name it "SiteRouteHandler.cs", and add the following code to this file.

using System;
using System.Web;
using System.Web.Routing;
using System.Web.Compilation;
using System.Web.UI;
public class SiteRouteHandler : IRouteHandler
{    
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        Page page;
        page = BuildManager.CreateInstanceFromVirtualPath(PageVirtualPath,
               typeof(Page)) as Page;
        foreach (var item in requestContext.RouteData.Values)
        {
            HttpContext.Current.Items["qparam." + item.Key] = item.Value;
        }
       return page; 
    }
    public string PageVirtualPath { get; set; }
}

After adding this bunch of code and the class file you need to go tho the Global.asax.cs page and add a namespace named "using System.Web.Routing" after this you need to add the following code to the page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;

namespace urlRewriting
{
    public class Global : System.Web.HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RegisterRoutes(RouteTable.Routes);
        }
        void RegisterRoutes(RouteCollection routes)
        {
            routes.Add("BlogPost", new Route("pages/corporate/",  
            new SiteRouteHandler() { PageVirtualPath = "~/Blogs/WebForm1.aspx" })
               );
            routes.Add("BlogPost1", new Route("pages/blog/",             
            new SiteRouteHandler() { PageVirtualPath = "~/Test/WebForm1.aspx" })
               );
            routes.Add("BlogPost2", new Route("pages/technology/test/check/",          
            new SiteRouteHandler() { PageVirtualPath = "~/Technology/WebForm1.
            aspx" }) );
        }
        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started

        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate           
             mode
            // is set to InProc in the Web.config file. If session mode is set           
            to StateServer 
            // or SQLServer, the event is not raised.

        }

    }
}                                                                                  


 Here I have masked 3 pages those are in 3 different folders Blog, Test and Technology.

The page in Blog folder is redirected to a virtual path "pages/corporate/".
The page in Test folder is redirected to a virtual path "pages/blog/".
The page in Technology folder is redirected to a virtual path "pages/technology/test/check/".

That's it, you have successfully masked your page urls to some virtual ones and search engine friendly urls.

Happy Coding...

17 comments:

  1. Hi there it's me, I am also visiting this web site daily, this site is actually good and the users are really sharing pleasant thoughts.
    See this: whirling.waw.pl, http://whining.wroclaw.pl

    ReplyDelete
  2. I do not even know how I stopped up right here, however I assumed this post was once good.
    I don't recognize who you are however certainly you're going to a well-known blogger if you happen to aren't already. Cheers!

    My page ... kliknij

    ReplyDelete
  3. Can I simply say what a relief to find somebody that truly understands what they're discussing on the internet. You certainly understand how to bring a problem to light and make it important. More and more people must look at this and understand this side of the story. I can't believe
    you're not more popular because you certainly possess the gift.

    my blog ... kliknij

    ReplyDelete
  4. Write more, thats all I have to say. Literally, it seems as though you relied on
    the video to make your point. You obviously know
    what youre talking about, why throw away your intelligence on just posting videos to your weblog when you could be giving us something enlightening to read?


    my web site: kliknij

    ReplyDelete
  5. I feel that is among the so much significant info for me.
    And i'm happy studying your article. But should statement on few basic issues, The site style is ideal, the articles is truly great : D. Good process, cheers

    Feel free to visit my web site - http://www.sancharam.com/Travel_Forum/profile.php?mode=viewprofile&u=62012

    ReplyDelete
  6. Write more, thats all I have to say. Literally, it
    seems as though you relied on the video to make your point.
    You definitely know what youre talking about, why waste
    your intelligence on just posting videos to your blog when
    you could be giving us something informative to read?



    Here is my web-site; kliknij

    ReplyDelete
  7. I am curious to find out what blog system you happen to be working with?
    I'm having some minor security problems with my latest blog and I would like to find something more secure. Do you have any suggestions?

    My site; kliknij

    ReplyDelete
  8. What's up to every one, it's genuinely a pleasant for
    me to pay a quick visit this web site, it contains priceless
    Information.

    Visit my blog post: kliknij

    ReplyDelete
  9. Thank you a bunch for sharing this with all folks you really recognise what you're speaking about! Bookmarked. Please additionally consult with my web site =). We will have a hyperlink exchange contract between us

    My web-site kliknij

    ReplyDelete
  10. This piece of writing offers clear idea for the new viewers of blogging, that in fact how to do blogging and site-building.


    my web page kliknij

    ReplyDelete
  11. Hi, its fastidious piece of writing on the topic of media print, we all know media is a fantastic source of facts.


    Also visit my web page; kliknij

    ReplyDelete
  12. My partner and I absolutely love your blog and find most of
    your post's to be just what I'm looking for. Does one offer guest writers to write
    content to suit your needs? I wouldn't mind composing a post or elaborating on many of the subjects you write with regards to here. Again, awesome site!

    Here is my page ... kliknij

    ReplyDelete
  13. Hello i am kavin, its my first time to commenting anywhere,
    when i read this post i thought i could also make comment due to this good paragraph.


    Also visit my site - kliknij

    ReplyDelete
  14. Good day! This is kind of off topic but I need
    some advice from an established blog. Is it
    hard to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking
    about creating my own but I'm not sure where to begin. Do you have any points or suggestions? Cheers

    Also visit my site - kliknij

    ReplyDelete
  15. It is not my first time to visit this web site, i am browsing this website dailly and take nice information from
    here daily.

    Have a look at my web site :: kliknij

    ReplyDelete
  16. I will right away grab your rss as I can not find your e-mail subscription link or newsletter
    service. Do you have any? Kindly permit me know so that I could subscribe.

    Thanks.

    Feel free to surf to my web blog i loved this ()

    ReplyDelete