URL Rewriting in ASP.NET [closed]
Could anyone suggest me which is the best technique for URL rewriting for an ASP.NET 3.5 web application?
Thanks
If you are using IIS 7 for your ASP.NET application, you may want to download:
- Microsoft's URL Rewrite Module for IIS 7.0
If you are using IIS 6 or IIS 5, you may want to consider using the following open source component:
- UrlRewritingNet.UrlRewrite
With which web server?
If it's the Apache HTTP server, then mod_rewrite is probably your best bet.
The Wikipedia article probably sums it up best, covering the major web servers and web frameworks.
There is also the option of URL Rewriter And Reverse Proxy, which is a mod_rewrite clone for IIS 6 and IIS 7, and works with ASP.NET.
If you are using ASP.NET 3.5 SP1, use ASP.NET UrlRouting instead. Check this great article from Chris Cavanagh or this one from Phil Haack.
Daniel Vassallo is correct in that you need to use UrlRewritingNet.UrlRewrite to make this work.
However, I am going to warn you now that most host (from my experience) are not correctly setup to handle URL Rewriting.
If you find that you are getting 404 errors, then either your URLRewrite setup is wrong in the web.config, or they have 'verify file exist' checked in IIS. Most host will change this for you if you request.
http://forums.asp.net/p/890825/1017645.aspx
http://blog.codeville.net/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/
The easiest solution is to pass all requests through the same script file (i.e. to index.php), and then parse the URL with script code. How you do this depends on your platform.
If you don't have the knowledge or the capability to install a URL re-writing plugin / module to your webserver or just need a simple re-write.
Below is a stripped down version of URL re-writing I do in IIS 6.
Here is a very basic version of URL rewriting that you can place into your Global.asax or Global.asax.cs file in the root of your website.
This snippet of code Redirects the url /Store/Categories/ to /Store/Default.aspx?action=categories while the user sees /Store/Categories/ in their URL, your application sees the URL as /Default.aspx?action=categories and Request["action"].ToString() will have the value of "categories"
void Application_BeginRequest(object sender, EventArgs e)
{
string fullOriginalpath = Request.Url.ToString().ToLower();
if (fullOriginalpath.Contains("/store/categories/"))
{
Context.RewritePath("/Store/Default.aspx?action=categories");
}
else if (fullOriginalpath.Contains("/store/products/"))
{
Context.RewritePath("/Store/Default.aspx?action=products");
}
}
精彩评论