Ali_dotNet
Ali_dotNet

Reputation: 3279

automatically rewrite URLs in ASP.NET

I use VS2010,C# to develop an ASP.NET web site, my customers want me to have their pages like this:

mysite.com/customer (in fact they call mysite/customer/default.aspx)

so I've manually created several folders for each customer, and inserted a default.aspx file in the folder so that users can view customer page by typing mysite.com/customer

is there a better way for performing this scenario? I don't want to have mysite.com/customer1.aspx, I want to have mysite.com/customer1, is there anyway that I can remove folders (and their containing default.aspx files) and generate something automatic using my customers database?

should I use URL rewriting? is there anyway that I can create page mysite.com/customer1.aspx, and users can view it by typing mysite.com/customer1?

I think it is possible to rewrite URLs in web.config, but I don't want to do it manually in web.config as my pages would increase in a daily basis

thanks

Upvotes: 1

Views: 1198

Answers (4)

EdSF
EdSF

Reputation: 12351

If you are (actually) willing to try url rewriting and such why not try ASP.net 4 Web Pages?

  • You can do Razor syntax in C# (cshtml) or VB (vbhtml).
  • You gain "pretty urls" by default (no configuration nor global.asax routing)
  • You can use Visual Studio or even WebMatrix

Possible challenges:

  • the (new) syntax - but its not that difficult - mostly @ instead of <%=..%>
  • if you're more comfortable with ASP.Net controls, then it may prove to be more difficult than much simpler...

Upvotes: 1

graham mendick
graham mendick

Reputation: 1839

ASP.NET Routing is the best way to do this, http://msdn.microsoft.com/en-us/library/cc668201.aspx

I've written a Navigation project that will help you as well, http://navigation.codeplex.com/ - if you're interested in this and need any help, let me know.

Upvotes: 1

Chris Simpson
Chris Simpson

Reputation: 8000

If you really really wanted to do this in code and not in the config you can rewrite the path yourself in the Application_BeginRequest using the RewritePath method of HttpContext

For example (and this is a very simplified example), if your aspx was in a subfolder the root it could go something like this:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    if (HttpContext.Current.Request.Path.StartsWith("/customer"))
        HttpContext.Current.RewritePath("/customer/whatever.aspx");
}

Note: This ignores any query string which must be extracted and re-added and if you want it to be case-insensitive, you've have to handle that too.

Upvotes: 2

Filip
Filip

Reputation: 2347

Config-based URL rewrite is your best option. Look here: http://msdn.microsoft.com/en-us/library/ms972974.aspx . The rewriting is pattern based, so what you are describing (mysite.com/customer1.aspx to mysite.com/customer1) is possible.

Upvotes: 1

Related Questions