To better organise my ASP.Net project I placed all my .aspx files in a folder called WebPages.
I would like to find a way to mask out the 'WebPages' folder out from all my URLs. So for example, I do not want to use the following URLs:
http://localhost:7896/WebPages/index.aspx
http://localhost:7896/WebPages/Admin/security.aspx
But instead, would like all my URLs to be as follows ('WebPages' is a physical folder that I use for structuring my work, but should not be visible to the outside world):
http://localhost:7896/index.aspx
http://localhost:7896/admin/security.aspx
I was able to come up with a solution of my own by specifiying routing entries 'for every page' that I have in my project (and it works), but that is simply not maintainable going forward and I need another method.
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "index.aspx", "~/WebPages/index.aspx");
routes.MapPageRoute("", "admin/security.aspx", "~/WebPages/Admin/security.aspx");
}
}
Perhaps what I am after is a class that catches all requests, and simply appends my 'WebPages' physical directory?
I finally went ahead with the following solution which works well for my situation:
In my Global.asax file, I have the following code:
public class Global : HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.Path.EndsWith(".aspx"))
{
FixUrlsForPages(Context, Request.RawUrl);
}
}
private void FixUrlsForPages(HttpContext context, string url)
{
context.RewritePath("/WebPages" + url);
}
}
It is pretty much doing what Tudor has suggested but in code instead of the web.config (which I couldn't get working).