Reputation: 3279
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
Reputation: 12351
If you are (actually) willing to try url rewriting and such why not try ASP.net 4 Web Pages?
Possible challenges:
@
instead of <%=..%>
Upvotes: 1
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
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
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