Asp.net 路由屏蔽路径中的物理文件夹

发布于 2024-11-04 11:17:54 字数 950 浏览 0 评论 0原文

为了更好地组织我的 ASP.Net 项目,我将所有 .aspx 文件放在名为 WebPages 的文件夹中。

我想找到一种方法从我的所有 URL 中屏蔽“WebPages”文件夹。例如,我不想使用以下 URL:

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

而是希望我的所有 URL 如下(“WebPages”是我用于构建工作的物理文件夹,但不应该对外部可见world):

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

我能够通过为我的项目中的“每个页面”指定路由条目(并且它有效)来提出自己的解决方案,但这根本无法维护,我需要另一种方法。

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

也许我想要的是一个捕获所有请求的类,并简单地附加我的“WebPages”物理目录?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

早茶月光 2024-11-11 11:17:54

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

你会在你的网络配置:

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

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>
堇色安年 2024-11-11 11:17:54

我最终继续采用以下解决方案,该解决方案非常适合我的情况:

在我的 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 中(我无法不去工作)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文