Asp.net @内容在一台服务器上不解析绝对路径,但在另一台服务器上解析绝对路径
我们目前在同一个域上有两个不同的服务器。但只有一台服务器解决了这个问题 @Url.Content(“~/api/User”)' 作为 其中,其他服务器无法绝对解决该问题;相反,它相对像 api/用户 代码库是相同的,我们使用的是MVC4。我不确定我们哪里出了问题,或者是否需要进行任何IIS/DNS设置才能修复此问题Asp.net @内容在一台服务器上不解析绝对路径,但在另一台服务器上解析绝对路径,asp.net,asp.net-mvc-4,razor-2,Asp.net,Asp.net Mvc 4,Razor 2,我们目前在同一个域上有两个不同的服务器。但只有一台服务器解决了这个问题 @Url.Content(“~/api/User”)' 作为 其中,其他服务器无法绝对解决该问题;相反,它相对像 api/用户 代码库是相同的,我们使用的是MVC4。我不确定我们哪里出了问题,或者是否需要进行任何IIS/DNS设置才能修复此问题 感谢所有的帮助;谢谢:)这与IIS web服务器中返回路径到http://domain.com/virtualdirectory/api/User 请看下面@Url.Content
感谢所有的帮助;谢谢:)这与IIS web服务器中返回路径到
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;
}
总之,如果请求和IsUrlRewriterTurnedOn都被重写
返回true,这意味着您的某个web服务器具有IIS重写模块
打开并运行,而另一个没有
有关ASP.NET MVC源代码的更多详细信息,请参阅以下链接:
http://aspnetwebstack.codeplex.com/
希望有帮助 尽管文档说这会生成一个绝对路径,但它就像内部实现返回一个在客户端工作正常的url一样(它调用
PathHelpers.GenerateClientUrl
)。您的第二个应用程序是安装在服务器的根目录中还是虚拟目录中?两者都作为应用程序安装,它们的应用程序池和权限都相同