
Signature
I have taken a vow of poverty. If you want to really piss me off, send me
money.
> 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?
Page is a member of Context, and Page is derived from Control:
Context.Page.ResolveUrl()

Signature
Riki
Lloyd Dupont - 19 Feb 2006 15:44 GMT
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 = HttpContext.Current;
string port = context.Request.ServerVariables["SERVER_PORT"];
if (port == null || port == "80" || port == "443")
port = "";
else
port = ":" + port;
string protocol = context.Request.ServerVariables["SERVER_PORT_SECURE"];
if (protocol == null || protocol == "0")
protocol = "http://";
else
protocol = "https://";
string ret = string.Format("{0}{1}{2}{3}",
protocol,
context.Request.ServerVariables["SERVER_NAME"],
port,
context.Request.ApplicationPath);
if (!ret.EndsWith("/"))
ret = ret + "/";
return ret;
}
public static string GetUrl(string file)
{
if (file.StartsWith("~/"))
file = file.Substring(2);
else if (file.StartsWith("/"))
file = file.Substring(1);
return HttpUtility.UrlPathEncode(GetRoot() + file);
}
}
>> I can't use Control.ResolveUrl because I need to write the conversion
>> in a utility class.
[quoted text clipped - 4 lines]
>
> Context.Page.ResolveUrl()
> 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?
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[] { '~' });
}

Signature
Hope this helps.
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Lloyd Dupont - 20 Feb 2006 00:04 GMT
Nice function.
And shorter that the one I wrote.
Thanks ;-)

Signature
Regards,
Lloyd Dupont
NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
>
>> I can't use Control.ResolveUrl because I need to write the
[quoted text clipped - 28 lines]
> relativeUrl.TrimStart(new char[] { '~' });
> }