【问题标题】:@Url.Content doesnt resolve absolute path on one server but does on another@Url.Content 不会在一台服务器上解析绝对路径,但会在另一台服务器上解析
【发布时间】:2014-01-20 07:48:25
【问题描述】:

我们目前在同一个域上有两台不同的服务器。但是一台服务器解决了

@Url.Content("~/api/User")'

作为

http://domain.com/virtualdirectory/api/User

因为其他服务器没有绝对解决它;而是它解决它相对像

api/用户

代码库相同,我们使用的是 MVC4。我不确定我们哪里出错了,或者是否需要进行任何 IIS/DNS 设置才能解决此问题。

感谢所有帮助;谢谢:)

【问题讨论】:

  • 虽然文档说这会生成一个绝对路径,但它就像内部实现返回一个在客户端可以正常工作的 url(它调用 PathHelpers.GenerateClientUrl)。您的第二个应用程序是安装在服务器的根目录中还是虚拟目录中?
  • 两者都是作为应用程序安装的,它们的AppPool和权限都是一样的

标签: asp.net asp.net-mvc-4 razor-2


【解决方案1】:

这与 IIS Web 服务器中的 IIS 重写模块有关,该模块返回 http://domain.com/virtualdirectory/api/User 的路径

看看下面@Url.Content的部分源码:

private static string GenerateClientUrlInternal(HttpContextBase httpContext, string contentPath)
{
     if (String.IsNullOrEmpty(contentPath))
     {
          return contentPath;
     }

     // can't call VirtualPathUtility.IsAppRelative since it throws on some inputs
     bool isAppRelative = contentPath[0] == '~';
     if (isAppRelative)
     {
           string absoluteContentPath = VirtualPathUtility.ToAbsolute(contentPath, httpContext.Request.ApplicationPath);
           return GenerateClientUrlInternal(httpContext, absoluteContentPath);
     }

     // we only want to manipulate the path if URL rewriting is active for this request, else we risk breaking the generated URL
     bool wasRequestRewritten = _urlRewriterHelper.WasRequestRewritten(httpContext);
     if (!wasRequestRewritten)
     {
            return contentPath;
     }

     // 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.
     string relativeUrlToDestination = MakeRelative(httpContext.Request.Path, contentPath);
     string absoluteUrlToDestination = MakeAbsolute(httpContext.Request.RawUrl, relativeUrlToDestination);
     return absoluteUrlToDestination;
}

使用以下代码检查您的网络服务器是否正在重写 URL:

bool requestWasRewritten = (httpWorkerRequest != null && httpWorkerRequest.GetServerVariable("IIS_WasUrlRewritten") != null);

还有:

private volatile bool _urlRewriterIsTurnedOnCalculated = false;
        private bool _urlRewriterIsTurnedOnValue;
        private object _lockObject = new object();
        private bool IsUrlRewriterTurnedOn(HttpContextBase httpContext)
        {
            // Need to do double-check locking because a single instance of this class is shared in the entire app domain (see PathHelpers)
            if (!_urlRewriterIsTurnedOnCalculated)
            {
                lock (_lockObject)
                {
                    if (!_urlRewriterIsTurnedOnCalculated)
                    {
                        HttpWorkerRequest httpWorkerRequest = (HttpWorkerRequest)httpContext.GetService(typeof(HttpWorkerRequest));
                        //bool urlRewriterIsEnabled = (httpWorkerRequest != null && httpWorkerRequest.GetServerVariable(UrlRewriterEnabledServerVar) != null);
                        bool urlRewriterIsEnabled = (httpWorkerRequest != null && httpWorkerRequest.GetServerVariable("IIS_UrlRewriteModule") != null);

                        _urlRewriterIsTurnedOnValue = urlRewriterIsEnabled;
                        _urlRewriterIsTurnedOnCalculated = true;
                    }
                }
            }
            return _urlRewriterIsTurnedOnValue;
        }

总之,如果 requestWasRewritten 和 IsUrlRewriterTurnedOn 返回 true,这意味着您的 Web 服务器之一具有 IIS 重写模块 打开并运行,而另一个没有。

更多ASP.NET MVC源码请参考此链接:

http://aspnetwebstack.codeplex.com/

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 2015-12-23
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 2020-05-27
    相关资源
    最近更新 更多