Asp.net 路由屏蔽路径中的物理文件夹
为了更好地组织我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 http://www.iis.net/download/urlrewrite 这个,
你会在你的网络配置:
Use http://www.iis.net/download/urlrewrite this instead
You would have this in your web.config:
我最终继续采用以下解决方案,该解决方案非常适合我的情况:
在我的 Global.asax 文件中,我有以下代码:
它几乎按照 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:
It is pretty much doing what Tudor has suggested but in code instead of the web.config (which I couldn't get working).