基本上是一样的。
许多使用 Url 作为参数的 SharePoint 方法(不是全部)将调用名为“GetWebRelativeUrlFromUrl”的内部方法来处理 Url
以下是调用 SPWeb.GetFile 时调用的方法的代码
如您所见,字符串将解析为 UriScheme 对象,如果您的字符串是 ServerRelative uriScheme,该方法会将其“转换”为绝对 url。
internal SPFile GetFile(string strUrl, byte iLevel)
{
string webRelativeUrlFromUrl = this.GetWebRelativeUrlFromUrl(strUrl);
if (webRelativeUrlFromUrl.Length == 0)
{
throw new ArgumentException();
}
return new SPFile(this, webRelativeUrlFromUrl, iLevel);
}
internal string GetWebRelativeUrlFromUrl(string strUrl)
{
return this.GetWebRelativeUrlFromUrl(strUrl, true, true);
}
internal string GetWebRelativeUrlFromUrl(string strUrl, bool includeQueryString, bool canonicalizeUrl)
{
string str;
char[] chrArray;
if (strUrl == null)
{
throw new ArgumentNullException();
}
if (strUrl.Length == 0)
{
return strUrl;
}
if (canonicalizeUrl || !includeQueryString)
{
strUrl = Utility.CanonicalizeFullOrRelativeUrl(strUrl, includeQueryString, out flag);
canonicalizeUrl = 0;
includeQueryString = 1;
}
UriScheme uriScheme = SPWeb.GetUriScheme(strUrl);
if (uriScheme == UriScheme.ServerRelative)
{
string serverRelativeUrl = this.ServerRelativeUrl;
if (!SPUtility.StsStartsWith(strUrl, serverRelativeUrl))
{
throw new ArgumentException();
}
str = strUrl.Substring(serverRelativeUrl.Length);
if (str.Length > 0)
{
if (str.get_Chars(0) == 47)
{
return str.Substring(1);
}
if (uriScheme == UriScheme.Http || uriScheme == UriScheme.Https)
{
if (uriScheme == UriScheme.Http && strUrl.Contains(":80/"))
{
strUrl = strUrl.Remove(strUrl.IndexOf(":80/"), ":80/".Length - 1);
}
bool flag2 = false;
if (!SPUtility.StsStartsWith(strUrl, this.Url))
{
using (SPSite sPSite = new SPSite(strUrl))
{
if (sPSite.ID != this.Site.ID)
{
throw new ArgumentException();
}
UriBuilder uriBuilder = new UriBuilder(new Uri(strUrl));
uriBuilder.Scheme = sPSite.Protocol.TrimEnd(new char[] { 58 });
uriBuilder.Host = sPSite.HostName;
uriBuilder.Port = sPSite.Port;
strUrl = uriBuilder.Uri.ToString();
}
flag2 = SPUtility.StsStartsWith(strUrl, this.Url);
}
else
{
flag2 = true;
}
if (flag2)
{
str = strUrl.Substring(this.Url.Length);
if (str.Length > 0)
{
if (str.get_Chars(0) == 47)
{
return str.Substring(1);
}
throw new ArgumentException();
if (!strUrl.StartsWith("_"))
{
bool flag3 = true;
try
{
if (false || -1 == strUrl.IndexOf(58) || null != new Uri(strUrl))
{
flag3 = false;
}
}
catch
{
}
if (!flag3)
{
throw new ArgumentException();
}
}
str = strUrl;
}
}
}
}
}
return str;
}