BRW*_*BRW 5 asp.net configuration routes
我有一个ASP.NET WebForms应用程序,其中包含与此文件结构类似的内容:
root\
default.aspx
web.config
subfolder\
page.aspx
web.config
Run Code Online (Sandbox Code Playgroud)
如果我page.aspx通过访问locahost/subfolder/page.aspx它来访问子文件夹中的web.config就好了.
但是,我有一个到页面设置的路径,如下所示:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "test", "~/subfolder/page.aspx");
}
Run Code Online (Sandbox Code Playgroud)
当我尝试通过该路由访问页面时,通过转到localhost/test,页面加载正常但它无法从子文件夹中的web.config读取值.
我错过了什么吗?是否有其他步骤允许子web.config使用路由?
我使用以下方法访问子web.config:
var test = WebConfigurationManager.AppSettings["testSetting"];
Run Code Online (Sandbox Code Playgroud)
我已经能够通过将以下内容添加到 Global.asax 来解决我的问题:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpRequest request = HttpContext.Current.Request;
Route route = RouteTable.Routes.Where(x => (x as Route)?.Url == request.Url.AbsolutePath.TrimStart('/')).FirstOrDefault() as Route;
if (route != null)
{
if (route.RouteHandler.GetType() == typeof(PageRouteHandler))
{
HttpContext.Current.RewritePath(((PageRouteHandler)route.RouteHandler).VirtualPath, request.PathInfo, request.Url.Query.TrimStart('?'), false);
}
}
}
Run Code Online (Sandbox Code Playgroud)
通过这样做,我伪造了 Request 对象的 Url 属性,以便对具有与现有页面路由匹配的 Url 的任何请求使用页面的“真实”URL。这样,当 WebConfigurationManager 提取配置(它通过当前虚拟路径执行)时,它会使用适当的页面提取它。
| 归档时间: |
|
| 查看次数: |
293 次 |
| 最近记录: |