ASP.NET MVC UrlHelper.GenerateUrl异常:“无法使用导航..退出
发布时间:2020-12-15 23:34:40 所属栏目:asp.Net 来源:网络整理
导读:我正在使用IIS 7 Rewrite模块来重写传入的URL,如: http://server/year/all 至 http://server/application/controller/year/all 一切正常,除了当处理重写的请求时,我使用MVC的UrlHelper.GenerateUrl()方法: UrlHelper.GenerateUrl( "Assets","Css","Asset",
我正在使用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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何阻止MVC缓存调用动作方法的结果?
- ASP.NET:隐藏gridview中的列
- asp.net – 如果Ninject尚未绑定,如何在Ninject中绑定?
- asp.net-mvc-4 – 在asp.net MVC URL中重写或更改路由?
- asp.net-mvc – 单元测试RedirectToRouteResult
- 使用ASP.NET SDK将文件上载到S3存储桶的文件夹
- asp.net – 限制关于AJAX调用的视图状态信息
- asp.net-mvc – WebAPI窗体身份验证SimpleMembershipProvid
- asp.net – 命名Dom元素的Id属性的最佳实践
- .net – SqlConnection的Dispose方法是否会干扰连接池?
推荐文章
站长推荐
- asp.net-mvc – Asp.Net MVC中的JQuery UI datep
- asp.net – 衡量Web请求和响应的大小
- asp.net-mvc – Bug? ASP.NET MVC 2中的客户端验
- asp.net – 要检查字符串值是否具有数值或C#
- asp.net core 使用 TestServer 来做集成测试
- 缓存 – 实体框架的缓存查询5
- asp.net – 什么是asp:DropDownList客户端事件?
- AspNet MVC中各种上下文理解
- asp.net-mvc-3 – MVC-Mini-Profiler错误地显示重
- 在asp.net httpContext.Cache中存储/检索项目最有
热点阅读