473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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.
Feb 19 '06 #1
4 2120
Lloyd Dupont wrote:
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()

--

Riki
Feb 19 '06 #2
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);
}
}


"Riki" <ri**@dontnagme.com> wrote in message news:uK****************@tk2msftngp13.phx.gbl...
Lloyd Dupont wrote:
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()

--

Riki

Feb 19 '06 #3
"Lloyd Dupont" <net.galador@ld> wrote in
news:u5**************@TK2MSFTNGP15.phx.gbl:
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.Requ est.ApplicationPath.Trim()).TrimEnd(new char[] { '/' }) +
relativeUrl.TrimStart(new char[] { '~' });
}
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Feb 19 '06 #4
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" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
"Lloyd Dupont" <net.galador@ld> wrote in
news:u5**************@TK2MSFTNGP15.phx.gbl:
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.Requ est.ApplicationPath.Trim()).TrimEnd(new
char[] { '/' }) +
relativeUrl.TrimStart(new char[] { '~' });
}
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Feb 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michael Stevens | last post by:
Probably the wrong wording but since I'm not a scripter I won't claim to know what I'm talking about. I got this script from www.htmlgoodies.com <script language="JavaScript"> <!--...
4
by: Bradley Plett | last post by:
I have what should be a trivial problem. I am using XMLSerializer to serialize an object. It serializes boolean values as "True" and "False". I then want to use an XSLT on this XML, and I want...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
5
by: Greg Collins [InfoPath MVP] | last post by:
I couldn't find anything in my searches... I'm wondering if there's a Regex (with or without additional C# code) that can convert a either "lowerCamelCase" or "UpperCamelCase" into a proper "Title...
2
by: privetv7 | last post by:
ppl... HELP!!!!! i don't know how to convert String into char..... for example i can do String input; int number; input = JOptionPane.showInputDialog( "Enter what ever" ); number =...
2
by: andrew007 | last post by:
I do xml / xslt transformation using asp.net but I found any value (w/xml format) in xml node html-encoded to &lt and &gt format if it's > or < tag. Since I have sub xml data in a parent xml node...
1
by: ygao | last post by:
in python dd = "\\xd6\\xd0\\xb9\\xfa" d = "\xd6\xd0\xb9\xfa" but how to convert one to other ? thanks.
7
by: 一首诗 | last post by:
Is there any simple way to solve this problem?
4
by: jiatiejun | last post by:
I want to convert a path from ~/xxx/xxx.gif to 'http://xxxxxxx/xxx/xxx.gif' how to convert it? thanks btw: the convert function allow
2
by: PW | last post by:
Doesn't look like Access 2003 has many date functions or a character to date function. Any ideas? Thanks, -paulw
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.