2

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?

Hady
  • 2,597
  • 2
  • 29
  • 34
  • I am working on a fairly large project with many controls and c# files, and we made the call to organise our files as such. It is not a standard I understand, but it is what works for us. – Hady May 02 '11 at 23:41
  • And it's your choice, of course. But I still feel quite strongly you are off track. Put your controls and C# files in subdirectories. If you create them with Visual Studio, it will prompt you to do this automatically. The root directory is for your core web pages. – Jonathan Wood May 02 '11 at 23:43
  • Thanks for the suggestion Jonathon. If I don't get this working I might revert to using the root directory as the location for my core web pages - although it will make things really messy for us – Hady May 02 '11 at 23:51

2 Answers2

0

Use http://www.iis.net/download/urlrewrite this instead

You would have this in your web.config:

<rewrite>
  <rules>
    <rule name="Rewrite to Webpages folder">
      <match url="(.*)" />
      <action type="Rewrite" url="/WebPages/{R:1}" />
    </rule>
  </rules>
</rewrite>
Tudor Carean
  • 972
  • 2
  • 12
  • 22
  • I am guessing the 'rewrite' element goes inside the 'system.webServer' element in the web.config file? I tried doing so in my project (even though vs2010 intellisense shows the structure in error) but it didn't work for me. – Hady May 02 '11 at 23:49
0

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).

Hady
  • 2,597
  • 2
  • 29
  • 34