ASP.NET MVC UrlHelper.GenerateUrl例外:"不能使用前导..退出顶级目录&QUOT以上; [英] ASP.NET MVC UrlHelper.GenerateUrl exception: "Cannot use a leading .. to exit above the top directory"

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

问题描述

我使用的是IIS 7重写模块重写传入的网址,如:

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

的http://服务器/年/所有

<一个href=\"http://server/application/controller/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例外:&QUOT;不能使用前导..退出顶级目录&QUOT以上;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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