Groups | Blog | Home
all groups > asp.net > february 2006 >

asp.net : How to convert a "~/xxx" url tp a client url?


Chris R. Timmons
2/19/2006 11:05:42 AM
"Lloyd Dupont" <net.galador@ld> wrote in
news:u5CSdjVNGHA.4052@TK2MSFTNGP15.phx.gbl:

[quoted text, click to view]

Lloyd,

Example Usage:

this.Label1.Text = GetFullUrl(HttpContext.Current, "~/folder1/default.aspx");

returns

http://localhost/WebApp1/folder1/default.aspx

Note:

This method works with cookieless sessions.



public string GetFullUrl(HttpContext context, string relativeUrl)
{
return
Uri.UriSchemeHttp +
Uri.SchemeDelimiter +
context.Request.Headers["Host"].ToLower() +
context.Response.ApplyAppPathModifier(context.Request.ApplicationPath.Trim()).TrimEnd(new char[] { '/' }) +
relativeUrl.TrimStart(new char[] { '~' });
}


--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
Riki
2/19/2006 3:23:01 PM
[quoted text, click to view]

Page is a member of Context, and Page is derived from Control:

Context.Page.ResolveUrl()

--

Riki

Lloyd Dupont
2/19/2006 11:32:09 PM
I can't use Control.ResolveUrl because I need to write the conversion in a
utility class.
However I know the current context.
How could I convert the URL to one usable by the user?

--
I have taken a vow of poverty. If you want to really piss me off, send me
money.

Lloyd Dupont
2/20/2006 12:00:00 AM
Nice function.
And shorter that the one I wrote.
Thanks ;-)

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
[quoted text, click to view]

Lloyd Dupont
2/20/2006 1:44:09 AM
Well I look in the documentation and HttpContext don't seem to have a =
Page member, so..
We'll I have to see if it compiles.

True to tell there is a CurrentHandler property though, which I could =
cast to a page.
But... although it's likely there would be a current page, it's not =
alway true.

Anyway I end up doing some code like that:

public class WebUrl
{
public static string GetRoot()
{
HttpContext context =3D HttpContext.Current;

string port =3D context.Request.ServerVariables["SERVER_PORT"];
if (port =3D=3D null || port =3D=3D "80" || port =3D=3D "443")
port =3D "";
else
port =3D ":" + port;

string protocol =3D =
context.Request.ServerVariables["SERVER_PORT_SECURE"];
if (protocol =3D=3D null || protocol =3D=3D "0")
protocol =3D "http://";
else
protocol =3D "https://";

string ret =3D string.Format("{0}{1}{2}{3}",
protocol,
context.Request.ServerVariables["SERVER_NAME"],
port,
context.Request.ApplicationPath);
if (!ret.EndsWith("/"))
ret =3D ret + "/";

return ret;
}
public static string GetUrl(string file)
{
if (file.StartsWith("~/"))
file =3D file.Substring(2);
else if (file.StartsWith("/"))
file =3D file.Substring(1);
=20
return HttpUtility.UrlPathEncode(GetRoot() + file);
}
}




[quoted text, click to view]
AddThis Social Bookmark Button