大佬教程收集整理的这篇文章主要介绍了asp.net – 子目录中的Web.config在使用页面路由时不起作用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
root\ default.aspx web.config subfolder\ page.aspx web.config
如果我通过访问locahost / subfolder / page.aspx来访问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"); }
当我尝试通过该路由访问页面时,通过转到localhost / test,页面加载得很好,但无法从子文件夹中的web.config读取值.
我错过了什么吗?是否有其他步骤允许子web.config使用路由?
我使用以下方法访问子web.config:
var test = WebConfigurationManager.AppSetTings["testSetTing"];
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); } } }
通过执行此操作,我伪造了request对象的Url属性,以使用与网页匹配现有网页路由的任何请求的页面的“真实”URl.这样,当WebConfigurationManager提取配置(它通过当前虚拟路径执行)时,它会使用适当的页面将其拉出.
以上是大佬教程为你收集整理的asp.net – 子目录中的Web.config在使用页面路由时不起作用全部内容,希望文章能够帮你解决asp.net – 子目录中的Web.config在使用页面路由时不起作用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。