@ Url.Content犯规解析一台服务器上的绝对路径,但的确在另一个 [英] @Url.Content doesnt resolve absolute path on one server but does on another

查看:149
本文介绍了@ Url.Content犯规解析一台服务器上的绝对路径,但的确在另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我们在同一个域中的两个不同的服务器。但有服务器解析


  

@ Url.Content(〜/ API /用户),



  

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


时,其它服务器犯规解决问题绝;而它解决它比较像


  

API /用户


在code碱基相同,我们使用的是MVC4。我不知道到哪里我们去错了,或者如果有需要,为了得到做任何IIS / DNS设置这个固定的。

所有帮助AP preciated;感谢:)


解决方案

这是在你的IIS Web服务器的路径返回到IIS重写模块相关的 http://domain.com/virtualdirectory / API /用户

就以@ Url.Content以下来源$ C ​​$ C的部分看看:

 私有静态字符串GenerateClientUrlInternal(HttpContextBase HttpContext的,字符串的contentPath)
{
     如果(String.IsNullOrEmpty(的contentPath))
     {
          返回的contentPath;
     }     //不能打电话VirtualPathUtility.IsAp prelative因为它会引发一些输入
     布尔ISAP prelative =的contentPath [0] =='〜';
     如果(ISAP prelative)
     {
           字符串absoluteContentPath = VirtualPathUtility.ToAbsolute(的contentPath,httpContext.Request.ApplicationPath);
           返回GenerateClientUrlInternal(HttpContext的,absoluteContentPath);
     }     //我们只希望,如果URL重写是活动此请求操纵的路径,否则我们就可能断裂生成的URL
     布尔wasRequestRewritten = _urlRewriterHelper.WasRequestRewritten(HttpContext的);
     如果(!wasRequestRewritten)
     {
            返回的contentPath;
     }     //由于rawUrl重新presents哪些用户可以看到在他的浏览器,它就是我们要为基数,以使用
     //我们的绝对路径。例如,考虑mysite.example.com/foo,这是内部
     //改写为content.example.com/mysite/foo。当我们要生成一个链接到〜/条,我们要
     //基地从/不是/ foo的,否则用户最终看到mysite.example.com/foo/bar,
     //这是不正确。
     字符串relativeUrlToDestination = MakeRelative(httpContext.Request.Path,的contentPath);
     字符串absoluteUrlToDestination = MakeAbsolute(httpContext.Request.RawUrl,relativeUrlToDestination);
     返回absoluteUrlToDestination;
}

使用的$ C $以下CS来检查你的web服务器是否具有URL重写:

 布尔requestWasRewritten =(的HttpWorkerRequest = NULL&放大器;!&安培; httpWorkerRequest.GetServerVariable(IIS_WasUrlRewritten)!= NULL);

也:

 私人挥发性布尔_urlRewriterIsTurnedOnCalculated = FALSE;
        私人布尔_u​​rlRewriterIsTurnedOnValue;
        私有对象_lockObject =新的对象();
        私人布尔IsUrlRewriterTurnedOn(HttpContextBase的HttpContext)
        {
            //需要做仔细检查锁因为这个类的一个实例,在整个应用程序域共享(见PathHelpers)
            如果(!_urlRewriterIsTurnedOnCalculated)
            {
                锁定(_lockObject)
                {
                    如果(!_urlRewriterIsTurnedOnCalculated)
                    {
                        的HttpWorkerRequest的HttpWorkerRequest =(的HttpWorkerRequest)httpContext.GetService(typeof运算(的HttpWorkerRequest));
                        //布尔urlRewriterIsEnabled =(的HttpWorkerRequest = NULL&放大器;!&安培; httpWorkerRequest.GetServerVariable(UrlRewriterEnabledServerVar)!= NULL);
                        布尔urlRewriterIsEnabled =(的HttpWorkerRequest = NULL&放大器;!&安培; httpWorkerRequest.GetServerVariable(IIS_UrlRewriteModule)!= NULL);                        _urlRewriterIsTurnedOnValue = urlRewriterIsEnabled;
                        _urlRewriterIsTurnedOnCalculated = TRUE;
                    }
                }
            }
            返回_urlRewriterIsTurnedOnValue;
        }


  

在总之,如果同时requestWasRewritten和IsUrlRewriterTurnedOn
  返回true,这意味着你的Web服务器中的一个已经IIS重写模块
  开启和运行时,而另一人不具有


有关ASP.NET MVC源$ C ​​$ CS,请参考以下链接更多的细节:

  HTTP://aspnetwebstack.$c$cplex.com/

希望它帮助!

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 :)

解决方案

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!

这篇关于@ Url.Content犯规解析一台服务器上的绝对路径,但的确在另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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