本文整理汇总了C#中Uri.MakeRelative方法的典型用法代码示例。如果您正苦于以下问题:C# Uri.MakeRelative方法的具体用法?C# Uri.MakeRelative怎么用?C# Uri.MakeRelative使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Uri
的用法示例。
在下文中一共展示了Uri.MakeRelative方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MakeRelative
// Return the relative vpath path from one rooted vpath to another
internal static string MakeRelative(string from, string to)
{
// If either path is app relative (~/...), make it absolute, since the Uri
// class wouldn't know how to deal with it.
from = MakeVirtualPathAppAbsolute(from);
to = MakeVirtualPathAppAbsolute(to);
// Make sure both virtual paths are rooted
Debug.Assert(IsRooted(from));
Debug.Assert(IsRooted(to));
// Uri's need full url's so, we use a dummy root
Uri fromUri = new Uri(dummyProtocolAndServer + from);
Uri toUri = new Uri(dummyProtocolAndServer + to);
return fromUri.MakeRelative(toUri);
}
示例2: RenderAttributes
protected override void RenderAttributes (HtmlTextWriter w)
{
/* Need to always render: method, action and id
*/
string action;
string customAction = Attributes ["action"];
Page page = Page;
HttpRequest req = page != null ? page.RequestInternal : null;
if (String.IsNullOrEmpty (customAction)) {
string file_path = req != null ? req.ClientFilePath : null;
string current_path = req != null ? req.CurrentExecutionFilePath : null;
if (file_path == null)
action = Action;
else if (file_path == current_path) {
// Just the filename will do
action = UrlUtils.GetFile (file_path);
} else {
// Fun. We need to make cookieless sessions work, so no
// absolute paths here.
bool cookieless;
SessionStateSection sec = WebConfigurationManager.GetSection ("system.web/sessionState") as SessionStateSection;
cookieless = sec != null ? sec.Cookieless == HttpCookieMode.UseUri: false;
string appVPath = HttpRuntime.AppDomainAppVirtualPath;
int appVPathLen = appVPath.Length;
if (appVPathLen > 1) {
if (cookieless) {
if (StrUtils.StartsWith (file_path, appVPath, true))
file_path = file_path.Substring (appVPathLen + 1);
} else if (StrUtils.StartsWith (current_path, appVPath, true))
current_path = current_path.Substring (appVPathLen + 1);
}
if (cookieless) {
Uri current_uri = new Uri ("http://host" + current_path);
Uri fp_uri = new Uri ("http://host" + file_path);
action = fp_uri.MakeRelative (current_uri);
} else
action = current_path;
}
} else
action = customAction;
if (req != null)
action += req.QueryStringRaw;
if (req != null) {
XhtmlConformanceSection xhtml = WebConfigurationManager.GetSection ("system.web/xhtmlConformance") as XhtmlConformanceSection;
if (xhtml == null || xhtml.Mode != XhtmlConformanceMode.Strict)
#if NET_4_0
if (RenderingCompatibilityLessThan40)
#endif
// LAMESPEC: MSDN says the 'name' attribute is rendered only in
// Legacy mode, this is not true.
w.WriteAttribute ("name", Name);
}
w.WriteAttribute ("method", Method);
if (String.IsNullOrEmpty (customAction))
w.WriteAttribute ("action", action, true);
/*
* This is a hack that guarantees the ID is set properly for HtmlControl to
* render it later on. As ugly as it is, we use it here because of the way
* the ID, ClientID and UniqueID properties work internally in our Control
* code.
*
* Fixes bug #82596
*/
if (ID == null) {
#pragma warning disable 219
string client = ClientID;
#pragma warning restore 219
}
string submit = page != null ? page.GetSubmitStatements () : null;
if (!String.IsNullOrEmpty (submit)) {
Attributes.Remove ("onsubmit");
w.WriteAttribute ("onsubmit", submit);
}
/* enctype and target should not be written if
* they are empty
*/
string enctype = Enctype;
if (!String.IsNullOrEmpty (enctype))
w.WriteAttribute ("enctype", enctype);
string target = Target;
if (!String.IsNullOrEmpty (target))
w.WriteAttribute ("target", target);
string defaultbutton = DefaultButton;
if (!String.IsNullOrEmpty (defaultbutton)) {
Control c = FindControl (defaultbutton);
if (c == null || !(c is IButtonControl))
throw new InvalidOperationException(String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.",
ID));
//.........这里部分代码省略.........
示例3: TestUriMakeRelative
public void TestUriMakeRelative()
{
Uri gnuphil = new Uri("http://www.gnu.org/philosophy/why-free.html");
Uri gnuoreilly = new Uri("http://www.gnu.org/gnu/thegnuproject.html");
Uri mozillaftp = new Uri("ftp://ftp.mozilla.org/pub/mozilla/latest/mozilla-i686-pc-linux-gnu-sea.tar.gz");
Uri mozillahttp = new Uri("http://ftp.mozilla.org/pub/mozilla/latest/mozilla-i686-pc-linux-gnu-sea.tar.gz");
Uri mandrake = new Uri("ftp://distro.ibiblio.org/pub/Linux/distributions/mandrake/Mandrake/iso/");
Uri debian = new Uri("ftp://distro.ibiblio.org/pub/Linux/distributions/debian/main/");
Uri debianrelease = new Uri("ftp://distro.ibiblio.org/pub/Linux/distributions/debian/main/source/Release");
AssertEquals(
"Code figures out simple relative Uri correctly (with files)",
gnuphil.MakeRelative(gnuoreilly),
"../gnu/thegnuproject.html");
AssertEquals("notices different schemes when comparing Uris",
mozillaftp.MakeRelative(mozillahttp),
mozillahttp.AbsoluteUri);
AssertEquals("figures out more complex, directory-based relative Uri",
mandrake.MakeRelative(debian),
"../../../debian/main/");
AssertEquals("tells difference between files and directorys, by looking for ending slash",
debianrelease.MakeRelative(debian), "../");
AssertEquals("correctly goes further into subdirectories",
debian.MakeRelative(debianrelease), "source/Release");
}
示例4: RenderAttributes
protected override void RenderAttributes (HtmlTextWriter w)
{
/* Need to always render: method, action and id
*/
/* The name attribute is rendered _only_ if we're not in
2.0 mode or if the xhtml conformance mode is set to
Legacy for 2.0 according to http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.name.aspx
*/
string action;
string customAction = Attributes ["action"];
Page page = Page;
HttpRequest req = page != null ? page.Request : null;
if (req == null)
throw new HttpException ("No current request, cannot continue rendering.");
#if !TARGET_J2EE
if (String.IsNullOrEmpty (customAction)) {
string file_path = req.ClientFilePath;
string current_path = req.CurrentExecutionFilePath;
if (file_path == current_path) {
// Just the filename will do
action = UrlUtils.GetFile (file_path);
} else {
// Fun. We need to make cookieless sessions work, so no
// absolute paths here.
bool cookieless;
SessionStateSection sec = WebConfigurationManager.GetSection ("system.web/sessionState") as SessionStateSection;
cookieless = sec != null ? sec.Cookieless == HttpCookieMode.UseUri: false;
string appVPath = HttpRuntime.AppDomainAppVirtualPath;
int appVPathLen = appVPath.Length;
if (appVPathLen > 1) {
if (cookieless) {
if (StrUtils.StartsWith (file_path, appVPath, true))
file_path = file_path.Substring (appVPathLen);
} else if (StrUtils.StartsWith (current_path, appVPath, true))
current_path = current_path.Substring (appVPathLen);
}
if (cookieless) {
Uri current_uri = new Uri ("http://host" + current_path);
Uri fp_uri = new Uri ("http://host" + file_path);
action = fp_uri.MakeRelative (current_uri);
} else
action = current_path;
}
} else
action = customAction;
action += req.QueryStringRaw;
#else
// Allow the page to transform action to a portlet action url
if (String.IsNullOrEmpty (customAction)) {
string queryString = req.QueryStringRaw;
action = CreateActionUrl (VirtualPathUtility.ToAppRelative (req.CurrentExecutionFilePath) +
(string.IsNullOrEmpty (queryString) ? string.Empty : "?" + queryString));
}
else
action = customAction;
#endif
XhtmlConformanceSection xhtml = WebConfigurationManager.GetSection ("system.web/xhtmlConformance") as
XhtmlConformanceSection;
if (xhtml != null && xhtml.Mode == XhtmlConformanceMode.Legacy)
w.WriteAttribute ("name", Name);
w.WriteAttribute ("method", Method);
if (String.IsNullOrEmpty (customAction))
w.WriteAttribute ("action", action, true);
/*
* This is a hack that guarantees the ID is set properly for HtmlControl to
* render it later on. As ugly as it is, we use it here because of the way
* the ID, ClientID and UniqueID properties work internally in our Control
* code.
*
* Fixes bug #82596
*/
if (ID == null) {
#pragma warning disable 219
string client = ClientID;
#pragma warning restore 219
}
string submit = page != null ? page.GetSubmitStatements () : null;
if (!String.IsNullOrEmpty (submit)) {
Attributes.Remove ("onsubmit");
w.WriteAttribute ("onsubmit", submit);
}
/* enctype and target should not be written if
* they are empty
*/
string enctype = Enctype;
if (!String.IsNullOrEmpty (enctype))
w.WriteAttribute ("enctype", enctype);
string target = Target;
if (!String.IsNullOrEmpty (target))
//.........这里部分代码省略.........
示例5: MakeRelative
// Return the relative vpath path from one rooted vpath to another
internal static string MakeRelative(string from, string to) {
// If either path is app relative (~/...), make it absolute, since the Uri
// class wouldn't know how to deal with it.
from = MakeVirtualPathAppAbsolute(from);
to = MakeVirtualPathAppAbsolute(to);
// Make sure both virtual paths are rooted
if (!IsRooted(from))
throw new ArgumentException(SR.GetString(SR.Path_must_be_rooted, from));
if (!IsRooted(to))
throw new ArgumentException(SR.GetString(SR.Path_must_be_rooted, to));
// Remove the query string, so that System.Uri doesn't corrupt it
string queryString = null;
if (to != null) {
int iqs = to.IndexOf('?');
if (iqs >= 0) {
queryString = to.Substring(iqs);
to = to.Substring(0, iqs);
}
}
// Uri's need full url's so, we use a dummy root
Uri fromUri = new Uri(dummyProtocolAndServer + from);
Uri toUri = new Uri(dummyProtocolAndServer + to);
string relativePath;
// VSWhidbey 144946: If to and from points to identical path (excluding query and fragment), just use them instead
// of returning an empty string.
if (fromUri.Equals(toUri)) {
int iPos = to.LastIndexOfAny(s_slashChars);
if (iPos >= 0) {
// If it's the same directory, simply return "./"
// Browsers should interpret "./" as the original directory.
if (iPos == to.Length - 1) {
relativePath = "./";
}
else {
relativePath = to.Substring(iPos + 1);
}
}
else {
relativePath = to;
}
}
else {
// To avoid deprecation warning. It says we should use MakeRelativeUri instead (which returns a Uri),
// but we wouldn't gain anything from it. The way we use MakeRelative is hacky anyway (fake protocol, ...),
// and I don't want to take the chance of breaking something with this change.
#pragma warning disable 0618
relativePath = fromUri.MakeRelative(toUri);
#pragma warning restore 0618
}
// Note that we need to re-append the query string and fragment (e.g. #anchor)
return relativePath + queryString + toUri.Fragment;
}
示例6: TestMakeRelative
public static void TestMakeRelative()
{
// Create a base Uri.
Uri address1 = new Uri("http://www.contoso.com/");
Uri address2 = new Uri("http://www.contoso.com:8000/");
Uri address3 = new Uri("http://[email protected]/");
// Create a new Uri from a string.
Uri address4 = new Uri("http://www.contoso.com/index.htm?date=today");
#pragma warning disable 0618
// Determine the relative Uri.
string uriStr1 = address1.MakeRelative(address4);
string uriStr2 = address2.MakeRelative(address4);
string uriStr3 = address3.MakeRelative(address4);
#pragma warning restore 0618
Assert.Equal(uriStr1, @"index.htm");
Assert.Equal(uriStr2, @"http://www.contoso.com/index.htm?date=today");
Assert.Equal(uriStr3, @"index.htm");
}
示例7: TestMakeRelative_Invalid
public static void TestMakeRelative_Invalid()
{
var baseUri = new Uri("http://www.domain.com/");
var relativeUri = new Uri("/path/", UriKind.Relative);
#pragma warning disable 0618
Assert.Throws<ArgumentNullException>("toUri", () => baseUri.MakeRelative(null)); // Uri is null
Assert.Throws<InvalidOperationException>(() => relativeUri.MakeRelative(baseUri)); // Base uri is relative
Assert.Throws<InvalidOperationException>(() => baseUri.MakeRelative(relativeUri)); // Uri is relative
#pragma warning restore 0618
}