For a more user-friendly environment, I wish to have the page extensions removed and query strings to not be visible. For example, from http://www.example.com/news.aspx?id=123 I want it to be http://www.example.com/news/123. How can I obtain it?
I also wanted to take opportunity of this question to put two other questions:
- When using the URL rewriting, do I have to specify a rule for each page?
- Which is better to use for URL rewriting in ASP.NET – the
.htaccessfile or the setting of rules in theweb.config?
Thanks.
You can use the URL Routing classes that were introduced in ASP.Net MVC for this purpose. These classes can be used in any .Net web application and are not limited to the MVC release.
With the “System.Web.Routing” namespace in place you can define routes as rules such as;
You register these “routes” in your global.asax application startup event in the same manners as you would in MVC.
This would allow http://www.domain.com/news/123 to be interpreted in your web application in the same manner as http://www.domain.com/news.aspx?id=123.
Here’s a recent article on using the MVC routing namespace in a traditional aspx application.