gpt4 book ai didi

ASP.NET MVC UrlHelper.GenerateUrl 异常 : "Cannot use a leading .. to exit above the top directory"

转载 作者:行者123 更新时间:2023-12-02 02:11:35 25 4
gpt4 key购买 nike

我正在使用 IIS 7 重写模块来重写传入的 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 中),因为它已经过编译器优化。

有什么想法吗?

最佳答案

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

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 UrlHelper.GenerateUrl 异常 : "Cannot use a leading .. to exit above the top directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3826299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com