ASP.NET MVC UrlHelper.GenerateUrl 异常:“无法使用前导 .. 退出顶级目录" [英] ASP.NET MVC UrlHelper.GenerateUrl exception: "Cannot use a leading .. to exit above the top directory"

查看:20
本文介绍了ASP.NET MVC UrlHelper.GenerateUrl 异常:“无法使用前导 .. 退出顶级目录"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 IIS 7 重写模块来重写传入的 url,例如:

I am using the IIS 7 Rewrite module to rewrite an incoming url like:

http://server/year/all

http://server/application/controller/year/all

一切正常,除了在处理重写的请求时,我使用 MVC 的 UrlHelper.GenerateUrl() 方法:

Everything works fine, except when, while processing the rewritten request, I use MVC's UrlHelper.GenerateUrl() method:

UrlHelper.GenerateUrl(
   "Assets",
   "Css",
   "Asset",
   new RouteValueDictionary(new { site = site.Name, assetPath = assetPath }),
   RouteTable.Routes,
   controllerContext.RequestContext,
   false);

调用此方法会导致 HttpException:

Calling this method results in an 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,似乎所有的请求路径都是正确的(即具有重写的值).我似乎无法弄清楚为什么它试图退出顶级目录...我们在路径中没有使用....

Looking at the RequestContext, it seems that all of the request paths are correct (ie, have the rewritten values). I can't seem to figure out why it's trying to exit out of the top level directory... There's nowhere we are using .... in a path.

我还确保 RewriteModule 位于 IIS 中 UrlRouting 模块的上方.

I've also made sure the RewriteModule is in above the UrlRouting module in IIS.

虽然我可以进入框架方法,但我无法检查任何局部变量(在 VS 或 WinDbg 中),因为它已经过编译器优化.

While I can step into the framework methods, I can't examine any of the local variables (either in VS or WinDbg) because it's been compiler optimized.

有什么想法吗?

推荐答案

这是一个涉及私有实现细节的怪诞解决方法,但添加以下内容:

This is a grotesque workaround involving private implementation details, but add this:

HttpContext.Current.Request.ServerVariables.Remove("IIS_WasUrlRewritten");

这避免了在 PathHelper.GenerateClientUrlInternal 中进行的内部检查,以查看请求是否被重写.正如参考资料中的这条评论所暗示的那样,这很可能会破坏某些场景:

This avoids the internal check done in PathHelper.GenerateClientUrlInternal to see if the request was rewritten. It's quite likely that this will break some scenarios, as hinted at by this comment in the reference sources:

// 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 UrlHelper.GenerateUrl 异常:“无法使用前导 .. 退出顶级目录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆