Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lloyd Dupont - 19 Feb 2006 13:32 GMT
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?

Signature

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

Riki - 19 Feb 2006 14:23 GMT
> 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()
Chris R. Timmons - 19 Feb 2006 19:05 GMT
> 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[] { '~' });
>  }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.