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

对着背影说爱祢 提交于 2019-12-13 00:37:45

问题


We currently have two different servers on same domain. But one server resolves

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

as

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

where as other server doesnt resolve it absolutely; rather it resolves it relatively like

api/user

The code base is same and we are using MVC4. I am not sure as to where we went wrong or if there is any IIS/DNS settings that need to be done in order to get this fixed.

All help is appreciated; thanks :)


回答1:


This is related with the IIS Rewriting module in your IIS web server that return the path to http://domain.com/virtualdirectory/api/User

Take a look on the part of source code of @Url.Content below:

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

Use the codes below to check whether your web servers are having the URL rewritten:

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

And Also:

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

In summary, If both requestWasRewritten and IsUrlRewriterTurnedOn return true, that means one of your web server has IIS Rewrite Module turned on and running while the other one doesn't have.

For more details on ASP.NET MVC source codes, please refer to this link:

http://aspnetwebstack.codeplex.com/

Hope it helps!



来源:https://stackoverflow.com/questions/21228660/url-content-doesnt-resolve-absolute-path-on-one-server-but-does-on-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!