This article explains an easy way in ASP.NET to do a MOD URL base from a table in the database.
- URL Rewriter solution
- Sample Web Project
- Table scripts
- Web.Config file
- SQL scripts to create a Stored Procedure and database tables
You can create a URL rewriting component for ASP.NET.
The two site examples are:
I needed a way to create nice URLs that search engines like for a content page. The goal of this was to be able to add content pages to a site and automatically have the URL rewritten and pulled from a database.
The content site has an article summary page and an article detail page. Without URL rewriting, the links, as the search engine would see them, are:
A couple reasons we want to change this:
- Search engines like words.
- It is not apparent to the user what the link is.
- If you email the link, it may be truncated in the email.
Look how much better these URLs look:
That is the goal of this project.
Note: Much thanks to Scott Mitchell from 4GuysFromRolla.com. He created the base set of code that does the hard work. I just extended it to work with a database.
This is the original article this is based on:
Now, let's see how to do this.
Add a reference to URLWriter.dll to your ASP.NET project (1.0 or 2.0 doesn't matter). It is already compiled and ready to go in the download.
Add the following to your web.config. This tells ASP.NET to send the request through the
The database connection to look up the URLs in a table:
value="Data Source=[YourSQLServer];Initial Catalog=[DataBaseName];
This tells ASP.NET what to use for the HTTP modules:
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
There is a sample web.config in the download for this source code.
Create the tables in the database using the script URLWriterDBTableScripts.sql. This creates two tables and Stored Procedures. The Stored Procedures are called by the URLWriter.dll.
The page table is "PageMod" which lists the "LookFor" and "SendTo". When a request is made, the HTTP handlers will look for the URL in the dataset, and if it finds it, it sends it to "sendTo". If it doesn't find it, it passes the request on normally.
This creates two tables:
- PageMod: holds the "LookFor" and "SendTo" columns for the URL rewriter engine.
- PageContentLive: This table holds the content for the site. Notice the "PageMod" column, and the keywords, description.
This is a template table, so you can create a page that has a list of the links, to the content, and display the links to the user. One trick is to have a trigger on the "On Insert" event of a record, to auto populate the records in the PageMod table.
Set the connection string in the web.config file for the URL Rewriter. That is the basic setup. If you want to look at the code, have a look at it. Let me know if there are any changes that would make it better.
Now, here is what you need to put into the table to test. The syntax must be exact:
When the request comes in for:
It is sent to:
So in the table, you would have:
[SAMPLE LOOK FOR ROOT]
The final slash before the .aspx is so the Regular Expression engines can quickly look for it.
[SAMPLE SEND TO ROOT]
So that is the basics of how the thing works. This is my first article, so please let me know if it all works.
Source code information
In the source code:
GetConfig: This is where the settings are grabbed from the database.
- This is the data access component to access the database: MODDALC.cs.
- Appconfig setting:
Let me know if this helps and what we can do to extend the functionality.