You are here:
VirtualUrl.NET: Examples of Use
Creating data-driven ASP.NET websites often results in a single web page that displays multiple content coming from the database and based on URL querystring parameters. By using VirtualUrl.NET you no longer have to use querystrings and your URL become shorter and easier to remember. The following examples show you the primary use of VirtualUrl.NET.
- Example 1: Single querystring parameter replacement
- Example 2: Multiple querystring parameters replacement
- Example 3: External Links Counter
- Example 4: Friendly Member Profile's URL
- Example 5: Storing Static Rules Hierarchically
- Example 6: 404 Error Catching
- Example 7: Using Response Filters
- Example 8: Working with Querystring parameter Lookup
Example 1: Single querystring parameter replacement
| Original URL | New Virtual URL | |
|---|---|---|
| /products/item.aspx?id=1 | /products/apple.aspx | |
| /products/item.aspx?id=1 | /products/apple | |
Using Static Rules Configuration in Sitemap.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <sitemap> 3 <website> 4 5 <page title="Products - Apples" urlmask="~/products/apples" 6 url="~/products/item.aspx?id=1" mask="true" enabled="true" /> 7 8 <page title="Products - Apples" urlmask="~/products/apples.aspx" 9 url="~/products/item.aspx?id=1" mask="true" enabled="true" /> 10 11 </website> 12 </sitemap>
Using Dynamic Rules Configuration in Sitemap.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <sitemap> 3 <extension> 4 5 <case match="~/products/(?'name'\w+)" url="~/products/item.aspx?name=$name" /> 6 7 </extension> 8 </sitemap>
Example 2: Multiple querystring parameters replacement
| Original URL | New Virtual URL | |
|---|---|---|
| /news/display.aspx?year=2004&month=1&day=12 | /news/2004/1/12 | |
Rules Configuration in Sitemap.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <sitemap> 3 <extension> 4 5 <case match="/news/(?'year'\d{4})/(?'month'\d{2})/(?'day'\d{2})" 6 url="/news/display.aspx?year=${year}&month=${month}&day=${day}" /> 7 8 </extension> 9 </sitemap>
Example 3: External Links Counter
If you wish to count your external links for analysis, by using VirtualUrl.NET your server will log every request to the virtual links and your users will be redirected to the external URLs. When redirecting to external URLs, remember to set 'mask' attribute to false to perform a simple 302 HTTP Redirection.
| Original URL | New Virtual URL | |
|---|---|---|
| http://www.asp.net/Default.aspx?tabindex=3&tabid=45 | /aspnet/tutorials | |
| http://search.yahoo.com/search?p=visionalyse | /yahoo/visionalyse | |
| http://babelfish.altavista.com | /translate | |
Rules Configuration in Sitemap.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <sitemap> 3 <website> 4 <page title="ASP.NET Tutorials" urlmask="~/aspnet/tutorials" url="http://www.asp.net/Default.aspx?tabindex=3&tabid=45" 5 mask="false"/> 6 <page title="Babelfish Translation " urlmask="~/translate" url="http://babelfish.altavista.com" mask="false"/> 7 </website> 8 <extension> 9 <case match="/yahoo/(?'keyword'\w+)" 10 url="http://search.yahoo.com/search?p=${keyword}"> 11 </case> 12 </extension> 13 </sitemap>
Example 4: Friendly Member Profile's URL
If your web site is a community site with member profiles, offering them a friendly profile URL will look more professional and increase your popularity as members will then remember their profile page and promote it elsewhere.
| Original URL | New Virtual URL | |
|---|---|---|
| /members/profile.aspx?user=paul | /paul | |
| /members/profile.aspx?user=james | /james | |
1 <?xml version="1.0" encoding="utf-8" ?> 2 <sitemap> 3 <extension> 4 5 <case match="/(?'username'\w+)" url="/members/profile.aspx?user=${username}" /> 6 7 </extension> 8 </sitemap>
Example 5: Storing Static Rules Hierarchically
VirtualUrl.NET allows static rules to be created hierarchically so you can keep your configuration file clean and easy to understand. By adding all your redirections and even links on your site you can generate a dynamic sitemap page from the configuration file. This example shows you a small-medium web site configuration rules.
| Original URL | New Virtual URL | |
|---|---|---|
| /about.aspx | /about.aspx | |
| /news.aspx | /news.aspx | |
| /news-item.aspx?id=1 | /news/1.aspx | |
| /products.aspx | /products.aspx | |
| /products-fruits.aspx | /products/fruits.aspx | |
| /products-apples.aspx | /products/fruits/apples.aspx | |
| /products-oranges.aspx | /products/fruits/oranges.aspx | |
| /products-cereales.aspx | /products/cereales.aspx | |
| /products-nuts.aspx | /products/cereales/nuts.aspx | |
| /contact.aspx | /contact.aspx | |
Rules Configuration in Sitemap.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <sitemap> 3 <website> 4 5 <page title="About Us" urlmask="~/about.aspx" url="/about.aspx" enable="false" /> 6 7 <page title="News" urlmask="~/news.aspx" url="/news.aspx" enable="false" /> 8 9 <page title="Products" urlmask="~/products.aspx" url="/products.aspx" enable="false"> 10 11 <page title="Products - Fruits" urlmask="~/products/fruits.aspx" url="/products-fruits.aspx"> 12 <page title="Products - Fruits - Apples" urlmask="~/products/fruits/apples.aspx" url="/products-apples.aspx"/> 13 <page title="Products - Fruits - Oranges" urlmask="~/products/fruits/oranges.aspx" url="/products-oranges.aspx"/> 14 </page> 15 16 <page title="Products - Cereales" urlmask="~/products/cereales.aspx" url="/products-cereales.aspx"> 17 <page title="Products - Cereales - Nuts" urlmask="~/products/cereales/nuts.aspx" url="/products-nuts.aspx"/> 18 </page> 19 20 </page> 21 22 <page title="Contact Us" urlmask="~/contact.aspx" url="/contact.aspx" enable="false" /> 23 24 </website> 25 26 <extension> 27 <case match="/news/(?'id'\d{8}).aspx" url="/news-item.aspx?id=${id}" /> 28 </extension> 29 30 </sitemap>
Example 6: 404 Error Catching
With a little bit of imagination, VirtualUrl.NET can be used as an error catching engine. By creating a dynamic rule which would match any URL, instead of displaying a 404 error for pages that cannot be found, you could redirect the user to a search page, parse the URL the user was trying to access and display search results.
The following example shows how to configure your rule to allow the error catching:
| URL Generating 404 errors | Error Catching URL | |
|---|---|---|
| /help.aspx | /search.aspx?keyword=help.aspx | |
| /products/apples.aspx | /search.aspx?keyword=products/apples.aspx | |
Rules Configuration in Sitemap.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <sitemap> 3 <extension> 4 <case match="~/(?'undefined'.+)" url="~/search.aspx?keyword=${undefined}" /> 5 6 </extension> 7 </sitemap>
Example 7: Using Response Filters
Response filters have been introduced in version 2.0 of VirtualUrl.NET
By using the response filters, you can now modify your hyperlinks in run-time. This example will show you how to use them to replace querystring parameters in run-time mode.
Objective 1: Single Querystring Parameters
| Orginal URL | New Virtual URL | |
|---|---|---|
| /products.aspx?id=422 | /products/422.aspx | |
| /products.aspx?id=1997 | /products/1997.aspx | |
Rules Configuration in Sitemap.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <sitemap> 3 <extension> 4 5 <case match="~/products/(?'id'\d+).aspx" 6 url="~/products.aspx?id=${id}"> 7 8 <responseFilter 9 match="~/products.aspx\?id=(?'id'\d+)" 10 url="~/products/${id}.aspx"> 11 </responseFilter> 12 13 </case> 14 15 </extension> 16 <settings> 17 18 <responseFilters mode="DynamicRules"> 19 <add tag="a" attribute="href" /> 20 </responseFilters> 21 22 </settings> 23 </sitemap>
Objective 2: Multiple Nested Querystring Parameters
| Orginal URL | New Virtual URL | |
|---|---|---|
| /products.aspx?cat=1&id=422 | /products/1/422.aspx | |
| /products.aspx?cat=4&id=1997 | /products/4/1997.aspx | |
Rules Configuration in Sitemap.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <sitemap> 3 <extension> 4 5 <case match="~/products/(?'cat'\d+)/(?'id'\d+).aspx" 6 url="~/products.aspx?cat=${cat}&id=${id}"> 7 8 <responseFilter 9 match="~/products.aspx\?cat=(?'cat'\d+)&id=(?'id'\d+)" 10 url="~/products/${cat}/${id}.aspx"> 11 </responseFilter> 12 13 </case> 14 15 </extension> 16 <settings> 17 18 <responseFilters mode="DynamicRules"> 19 <add tag="a" attribute="href" /> 20 </responseFilters> 21 22 </settings> 23 </sitemap>
Example 8: Working with Querystring parameter Lookup
With the new response filter functionality, you can now modify your old URL with querystring parameters into meaningful and friendly URL... and All this at the run-time.
| Orginal URL | New Virtual URL | |
|---|---|---|
| /products.aspx?cat=1&id=422 | /products/cereales/nuts.aspx | |
| /products.aspx?cat=4&id=1997 | /products/fruits/oranges.aspx | |
Rules Configuration in Sitemap.config
1 <?xml version="1.0" encoding="utf-8" ?> 2 <sitemap> 3 <extension> 4 5 <case match="~/products/(?'cat'\d+)/(?'id'\d+).aspx" 6 url="~/products.aspx?cat=${cat}&id=${id}"> 7 8 <responseFilter 9 match="~/products.aspx\?cat=(?'cat'\d+)&id=(?'id'\d+)" 10 url="~/products/${cat}/${id}.aspx"> 11 12 <parameter name="id" lookupSource="~/data/lookup.aspx" /> 13 <parameter name="cat" lookupSource="~/data/lookup.aspx" /> 14 15 </responseFilter> 16 17 </case> 18 19 </extension> 20 <settings> 21 22 <responseFilters mode="DynamicRules"> 23 <add tag="a" attribute="href" /> 24 </responseFilters> 25 26 </settings> 27 </sitemap>
Lookup Source File Content (XML Format)
1 <?xml version="1.0" encoding="utf-8" ?> 2 <lookupSource> 3 <parameter name="id"> 4 <set key="422" value="nuts" /> 5 <set key="150" value="apples" /> 6 <set key="1997" value="oranges" /> 7 </parameter> 8 <parameter name="cat"> 9 <set key="1" value="cereales" /> 10 <set key="2" value="vegetables" /> 11 <set key="4" value="fruits" /> 12 </parameter> 13 </lookupSource>
Notes: in database driven environments, the lookup source file should contain all parameter values.
