Skip Navigation LinksHome > Categories > Code from a Category

Live asp.net website example: how to use URLRewriter



User Name: jamest85
Name: James Tang
Contact Me: www.datawebcoder.com/ContactUs.aspx
Home Page: www.datawebcoder.com
Google Apps for Work Partner, Certified Zoho CRM Consultant. 7 years experience on Zoho consulting, implementation, and programming using C#, VB, Php..., 15 years of total IT experience including prog... [More]
Viewed Times: 2730
Add Date: 02/19/2010
This post will show how to use URLRewriter in this website.
If you have a look at the Address Box of this page, it is:
http://www.datawebcoder.com/codeByLanguage/1023/Live_example_URLRewriter_used_in_asp_net_page.aspx
codeByLanguage is a fake folder.
1023 is the real postId, very useful here.
Live_example_URLRewriter_used_in_asp_net_page is this post title, it is SEO friendly URL, that is my main purpose of doing the URLRewriter.
Behind scene, the real URL is:
http://www.datawebcoder.com/Languages/CodeInLang.aspx?id=1023
That link also can open the same page.

1. Install the UrlRewriter, after that, open your application (here I'm using Visual Studio 2005), right click your Bin folder, and add the reference of the .dll file.

2. You will get the .dll and related files in your Bin folder:

3. Create a SEO friendly URL. The result will be like this page, have taken off the css style and extra columns in the gridview here, above getLinkPath() is located in code behind and shows below: As we know, above link won't open any page because there is no such folder and page name in my application, we need use UrlRewriter (or other ways) to translate this URL to the one my application can accept, in this case, it is: http://www.datawebcoder.com/Languages/CodeInLang.aspx?id=1023, below steps show how:
4. Add code in .config file: when you download the UrlRewriter, you will get an instruction file (Readme.txt) and let you know what to do, however, you might need extra code, please have a look Missing part in .config file on installing urlrewriter to asp.net website for details.
5. In this example, the rewrite rule is:
This first (.+) will be the postId, second (.+) is the title name, $1 means the value in first (.+)
Below screen shoot shows you the fake but SEO friendly RawURL and the real Url when the real webpage opens:

6. Once we get this point, things are getting much easier, below shows how to get the Id when this page loads:
(http://www.datawebcoder.com/codeByLanguage/1023/Live_example_URLRewriter_used_in_asp_net_page.aspx ).
7. Extra side works: After UrlRewriter is working, your website might have problem showing images, or even your css style is gone, don't panic, I had this problem as others, make sure the style sheet path or image URL path is starting from root of your application, for example:
bad one: good one: 8. Keep the friendly URL after the page does the postback: if you have a button in the page which can cause a page postback, for example, there is a "Get a New One" button at the bottom of this page, if we click it, the SEO friendly URL will go back to the real URL, that is not what I want. Fortunately, we can solve this problem easily, just add "From.browser" and "FormRewriter.cs" to the website, you can download above C# version files here: DotNetGuts.
9. I also had a "Server 405 error" after I installed UrlRewriter, turns out I need to modify the FromRewriter.cs file a little bit, please read my another post: Server 405 error after URL rewritten and page does postback

If you have any comments or questions, let me know, thanks!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.