簡體   English   中英

@ Url.Content不會解析一台服務器上的絕對路徑,但會解析另一台服務器上的絕對路徑

[英]@Url.Content doesnt resolve absolute path on one server but does on another

目前,我們在同一域上有兩台不同的服務器。 但是一台服務器解析

@ Url.Content( “〜/ API /用戶”)”

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

其他服務器無法完全解決的地方; 而是相對地解決它

API /用戶

代碼庫是相同的,我們正在使用MVC4。 我不確定問題出在哪里,或者是否需要進行任何IIS / DNS設置才能解決此問題。

感謝所有幫助; 謝謝 :)

這與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;
}

使用以下代碼檢查您的Web服務器是否已重寫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;
        }

總之,如果requestWasRewrite和IsUrlRewriterTurnedOn都返回true,則意味着您的Web服務器之一已打開IIS Rewrite Module並正在運行,而另一服務器則沒有。

有關ASP.NET MVC源代碼的更多詳細信息,請參考以下鏈接:

http://aspnetwebstack.codeplex.com/

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM