Beruflich Dokumente
Kultur Dokumente
• HOME
• ARTICLES
• ADVERTISE
• ABOUT
• CONTACT
Aug24200821
0tweetsretweet
reminder on rewriting URLs. But, if you don't, please take this article seriously :)
Or you'll hate it after you realize how easy it is to make it nicer. Besides all, URL rewriting
will improve your rankings on search engines. Search engines like Google will easily index
your "static" URLs, instead of dynamic URLs.
There are several ways to accomplish URL Rewriting. I'll explain how to do this by using
HttpModule 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.
In the example in this article I will not use a database in order to keep it simple, but you
imagine there is a database that keeps the URL for each page. I'll use hard-coded Dictionary
that will keep some sample pages.
First, we'll make a data access object that will search the database for requested url and
return its dynamic url. These are the methods in SampleDAO that will simulate the database
and getting the url from there:
What does this means? When you create links on your web pages, you will no longer use
query parametrised URLs but rather static URLs. Now what? If you request a page that
doesn't exist, HttpException will be thrown or default redirection will occur, but this is the
place where Rewrite module comes in.
Rewrite module (in our Example UrlRewriteModule) implements IHttpModule interface. This
means you will have to implement two methods: Init and Dispose. Init method is more
important. It accepts HttpApplication and lets you hook on whatever you want. In our case
we'll hook on Application BeginRequest event so that we can read a requested URL each
time a new request begins its lifecycle.
Look at the code below. In the Application_BeginRequest event handler we get the
requested url which is a static URL, and search the database to find the corresponding
dynamic url.
if (!String.IsNullOrEmpty(realUrl))
app.Context.RewritePath(realUrl, false);
}
private string GetRealUrl(string requestedUrl)
{
There is one more thing to do before this handler becomes fully functional. We have to
register it in web.config file. We just have to add a new httpModule in the system.web
section. Under nameyou have to type it's class name, and under type you'll have to type it's
full assembly name. Since our module is in the current project in App_Code folder, we'll just
provide its name as a type.
<httpModules>
<add name="UrlRewriteModule" type="UrlRewriteModule"/>
</httpModules>
And that's all you need to make it work. However, there is a problem with postback when
you are using URL rewriting. Here's the problem: the action attribute of the server-
side form tag on your page will point to the original page, and it's url is not a static one, but
rather dynamic. So you will loose your URL rewrite functionality on each postback.
The solution is to "tweak" the output of the action attribute. This approach is known as
Anyway, we'll just have to add a new .browser file and register our
FormRewriterControlAdapter.
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
adapterType="FormRewriterControlAdapter" />
</controlAdapters>
</browser>
Conclusion
This is all you have to do to make URL rewriting work properly. The logic for getting the
corresponding dynamic URL for the requested one can be different. You can store both static
and dynamic paths in a database and perform a search, you can use regular expressions,
you can create the methods for parsing the requested URL and search for the item in the
database, or you can implement your own custom logic.
GO TOP
Bottom of Form