How to convert a "~/xxx" url tp a client url?

  • Thread starter Thread starter Lloyd Dupont
  • Start date Start date
L

Lloyd Dupont

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 said:
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()
 
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.
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[] { '~' });
}
 
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>
Chris R. Timmons said:
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[] { '~' });
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top