2011-05-02 113 views
2

为了更好地组织我的ASP.Net项目,我将所有的.aspx文件放在一个名为WebPages的文件夹中。Asp.net路由掩盖路径中的物理文件夹

我想找到一种方法从我所有的URL中屏蔽出'WebPages'文件夹。因此,举例来说,我不希望使用以下网址:

http://localhost:7896/WebPages/index.aspx 
http://localhost:7896/WebPages/Admin/security.aspx 

而是想我所有的网址如下(“网页”是,我使用的构建我的工作的物理文件夹,但应该是不可见的外部世界):

http://localhost:7896/index.aspx 
http://localhost:7896/admin/security.aspx 

我能够specifiying路由项“每一页”,我在我的项目拿出我自己的解决方案(和它的作品),但这是不可维护的,我需要另一种方法。

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"); 
    } 
} 

也许什么我以后是捕获所有请求类,并就追加我的“网页”的物理目录?

+0

我正在研究一个相当大的项目,其中包含许多控件和c#文件,并且我们打电话来组织我们的文件。这不是我理解的标准,但它适用于我们。 – Hady 2011-05-02 23:41:18

+0

当然,这是您的选择。但我仍然非常强烈地感到你偏离了轨道。将你的控件和C#文件放在子目录中。如果您使用Visual Studio创建它们,它会提示您自动执行此操作。根目录是用于核心网页的。 – 2011-05-02 23:43:43

+0

感谢Jonathon的建议。如果我没有得到这个工作,我可能会恢复使用根目录作为我的核心网页的位置 - 虽然它会使我们对于我们来说非常混乱 – Hady 2011-05-02 23:51:35

回答

0

我最后就做了如下的解决方案,我的情况运行良好:

在我Global.asax文件,我有以下代码:

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); 
    } 
} 

它几乎是在做Tudor的建议,但在代码中,而不是web.config(我无法工作)。

0

使用http://www.iis.net/download/urlrewrite这个代替

你将不得不在这你的web.config:

<rewrite> 
    <rules> 
    <rule name="Rewrite to Webpages folder"> 
     <match url="(.*)" /> 
     <action type="Rewrite" url="/WebPages/{R:1}" /> 
    </rule> 
    </rules> 
</rewrite> 
+0

我猜'重写'元素进入'system.webServer '在web.config文件中的元素?我在我的项目中尝试这样做(尽管vs2010 intellisense显示错误的结构),但它不适用于我。 – Hady 2011-05-02 23:49:53