ASP.NET MVC UrlHelper.GenerateUrl异常:“无法使用导航..退出顶部目录”
发布时间:2020-05-24 01:36:14 所属栏目:asp.Net 来源:互联网
导读:我正在使用IIS 7 Rewrite模块来重写传入的URL,如: http://server/year/all 至 http://server/application/controller/year/all 一切正常,除了当处理重写的请求时,我使用MVC的UrlHelper.GenerateUrl()方法: UrlHelper.GenerateUrl( Assets, Css,
|
我正在使用IIS 7 Rewrite模块来重写传入的URL,如: http://server/year/all 至 http://server/application/controller/year/all 一切正常,除了当处理重写的请求时,我使用MVC的UrlHelper.GenerateUrl()方法: UrlHelper.GenerateUrl(
"Assets","Css","Asset",new RouteValueDictionary(new { site = site.Name,assetPath = assetPath }),RouteTable.Routes,controllerContext.RequestContext,false);
调用此方法会导致HttpException: System.Web.HttpException: Cannot use a leading .. to exit above the top directory. at System.Web.Util.UrlPath.ReduceVirtualPath(String path) at System.Web.Util.UrlPath.Reduce(String path) at System.Web.VirtualPath.Combine(VirtualPath relativePath) at System.Web.VirtualPathUtility.Combine(String basePath,String relativePath) at System.Web.Mvc.PathHelpers.GenerateClientUrlInternal(HttpContextBase httpContext,String contentPath) at System.Web.Mvc.PathHelpers.GenerateClientUrl(HttpContextBase httpContext,String contentPath) at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName,String actionName,String controllerName,RouteValueDictionary routeValues,RouteCollection routeCollection,RequestContext requestContext,Boolean includeImplicitMvcValues) 看到RequestContext,似乎所有的请求路径都是正确的(即重写的值).我似乎无法弄清楚为什么它试图退出顶级目录…在路径中没有任何地方使用…. 我还确保RewriteModule位于IIS中的UrlRouting模块之上. 虽然我可以进入框架方法,但我无法检查任何本地变量(在VS或WinDbg中),因为它已被编译器优化. 有什么想法吗? 解决方法这是一个怪诞的解决方法,涉及私有实现细节,但添加一下:HttpContext.Current.Request.ServerVariables.Remove("IIS_WasUrlRewritten");
这样可以避免在PathHelper.GenerateClientUrlInternal中进行内部检查,以查看请求是否被重写.这很可能会打破一些场景,正如参考资料中的这个评论所暗示的那样: // Since the rawUrl represents what the user sees in his browser,it is what we want to use as the base // of our absolute paths. For example,consider mysite.example.com/foo,which is internally // rewritten to content.example.com/mysite/foo. When we want to generate a link to ~/bar,we want to // base it from / instead of /foo,otherwise the user ends up seeing mysite.example.com/foo/bar,// which is incorrect. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 多个DbContext,多个Database.SetInitialize
- asp.net-mvc – 获取文件夹中的文件
- asp.net-mvc – Url.Action如何从模型中添加参数值
- ASP.net HTTP 404 – 找不到文件而不是MaxRequestLength异常
- 如何在IIS上解决接近100%的CPU利用率
- asp.net-mvc – Gzip压缩无法运行ASP.net MVC5
- asp.net – 使用NLog记录当前页面的URL
- ASP.NET httpRedirect:重定向所有页面,除了一个
- ASP.NET如何在没有最终用户删除的情况下在屏幕上编写消息?
- asp.net-mvc – ASP.NET MVC IIS7 FireFox:尾部斜杠在URL中
