views:

296

answers:

6

Hi,

Could anyone suggest me which is the best technique for URL rewriting for an ASP.NET 3.5 web application?

Thanks

+2  A: 

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.

Christopher
i am developing in asp.net 3.5
Very useful for Apache but he seems to be using IIS.
Josiah Peters
Indeed, but the original question didn't specify at the time; I don't know whether it's bad form to delete my answer now that others have clarified, but before the "correct" answer has been accepted (and now that someone has edited my answer)...
Christopher
A: 

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.

Emil Vikström
Does it mean Redirecting the requests? if yes i do not think is the best solution for me.
No, just configure the server so all requests is handled by the same file. Not by ridercting the web browser, just on the server side.
Emil Vikström
+3  A: 

If you are using IIS 7 for your ASP.NET application, you may want to download:

If you are using IIS 6 or IIS 5, you may want to consider using the following open source component:

Daniel Vassallo
I am on a shared hosting i can not access directly IIS, any other suggestion? thanks
In that case, you may want to consider using the following open source component: http://www.urlrewriting.net/
Daniel Vassallo
Once the module is installed, it can be used through web.config. I've used shared hosts where it is already in IIS, if it is not already then ask them to install it and they will probably. I've used many methods and this one is the easiest for ASP.NET WebForms.
DavGarcia
A: 

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.

Mehdi Golchin
+1  A: 

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/

Clarence Klopfstein
A: 

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");
    }
}
Josiah Peters
By redirecting is the application going to be slower?
This seems to me a really easy solution, does it have any drawback?
I wouldn't say this is a comparison of whether or not my method is very efficient. I did some searching and found a similar method as mine. Its suggesting using this style for simple rules and a module for a more complex set of rules. http://www.developer.com/net/article.php/3703901/How-to-Implement-and-Utilize-URL-Rewriting-with-ASPNET.htm
Josiah Peters