469,908 Members | 2,213 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,908 developers. It's quick & easy.

Find Base URL Of My Web App?

This seems like a really, really dumb question, but I can't seem to find
the simple answer that it seems to deserve.

My C#/ASP.net 2.0 app needs to email a URL link back to the site from a
web page. Sending the email is no problem, but generating this URL is
giving me an annoying issue in testing because of the difference in URLs
between the VS web server and IIS.

Specifically, the URL that I need to send when testing locally (VS
built-in web server) is http://localhost:4323/Safely-Sent/Validate.aspx.
However, in IIS, I need http://Safely-Sent.com/Validate.aspx.

So, I'd like to have a magic function that returns the correct 'base'
url (http://localhost:4323/Safely-Sent/ or http://Safely-Sent.com) for
my running instance as appropriate, onto which I can append my page
name. I looked at Request object properties, and at UriBuilder, but
nothing looked like what's needed.

Is there something trivial that I'm overlooking?

Thanks in advance!
May 18 '07 #1
3 4242
http://west-wind.com/weblog/posts/269.aspx


"Alan Cohen" <am***@cornell.no.junk.please.eduwrote in message news:uP**************@TK2MSFTNGP06.phx.gbl...
This seems like a really, really dumb question, but I can't seem to find the simple answer that it seems to deserve.

My C#/ASP.net 2.0 app needs to email a URL link back to the site from a web page. Sending the email is no problem, but generating
this URL is giving me an annoying issue in testing because of the difference in URLs between the VS web server and IIS.

Specifically, the URL that I need to send when testing locally (VS built-in web server) is
http://localhost:4323/Safely-Sent/Validate.aspx. However, in IIS, I need http://Safely-Sent.com/Validate.aspx.

So, I'd like to have a magic function that returns the correct 'base' url (http://localhost:4323/Safely-Sent/ or
http://Safely-Sent.com) for my running instance as appropriate, onto which I can append my page name. I looked at Request object
properties, and at UriBuilder, but nothing looked like what's needed.

Is there something trivial that I'm overlooking?

Thanks in advance!

May 18 '07 #2
Thanks!

I've actually temporarily solved my problem by setting the VS web
server's virtual path to '/' - but I'll read the post you linked to
hopefully find a better solution.

Jon Paal [MSMD] wrote:
http://west-wind.com/weblog/posts/269.aspx


"Alan Cohen" <am***@cornell.no.junk.please.eduwrote in message news:uP**************@TK2MSFTNGP06.phx.gbl...
>This seems like a really, really dumb question, but I can't seem to find the simple answer that it seems to deserve.

My C#/ASP.net 2.0 app needs to email a URL link back to the site from a web page. Sending the email is no problem, but generating
this URL is giving me an annoying issue in testing because of the difference in URLs between the VS web server and IIS.

Specifically, the URL that I need to send when testing locally (VS built-in web server) is
http://localhost:4323/Safely-Sent/Validate.aspx. However, in IIS, I need http://Safely-Sent.com/Validate.aspx.

So, I'd like to have a magic function that returns the correct 'base' url (http://localhost:4323/Safely-Sent/ or
http://Safely-Sent.com) for my running instance as appropriate, onto which I can append my page name. I looked at Request object
properties, and at UriBuilder, but nothing looked like what's needed.

Is there something trivial that I'm overlooking?

Thanks in advance!

May 18 '07 #3
You may want to look at the System.Web.VirtualPathUtility class
(http://msdn2.microsoft.com/en-us/lib...thutility.aspx)
This class is specifically designed for working with ASP.Net Virtual paths
(paths that begin with a tilde (~)). A Virtual path is an application
root-relative path. For example, if you have a project at
http://localhost/Safely-Sent, and you're developing for
http://Safely-Sent.com, your application root path will differ from the root
path on the live site. That is, if you have an image named "foo.gif" in your
app's images folder, the root-relative path to that image on your dev
machine will be "/Safely-Sent/images/foo.gif" but on the live server it will
be "/images/foo.gif." By using a Virtual path, the virtual path will be the
same on both machines, i.e. "~/images/foo.gif".

The VirtualPathUtility class has a number of static members for working with
Virtual Paths. There are 2 in particular that I find very useful:

VirtualPathUtility.ToAbsolute(path)
VirtualPathUtility.ToAppRelative(path)

The ToAbsolute method will take a virtual path ("~/.....", or a relative
path ("../.....") and convert it to an absolute path ("/.....") which begins
at the web root. In addition, if you pass an absolute path to it, it remains
unchanged.

The ToAppRelative method will take an absolute path "/....." or a relative
path "../.....") and convert it to a Virtual path ("~/....")

For example, my web application stores URLs in a database. While working on
the app, I don't want to put bad URLs into the database, and if the location
of the web changes, I don't want to have to change all of the URLs. So, when
I insert a URL into my database, I use the ToAppRelative method to put
virtual paths into the database. My app then uses the ToAbsolute method to
translate these back into absolute paths in the web site when they are
fetched from the database.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Alan Cohen" <am***@cornell.no.junk.please.eduwrote in message
news:uP**************@TK2MSFTNGP06.phx.gbl...
This seems like a really, really dumb question, but I can't seem to find
the simple answer that it seems to deserve.

My C#/ASP.net 2.0 app needs to email a URL link back to the site from a
web page. Sending the email is no problem, but generating this URL is
giving me an annoying issue in testing because of the difference in URLs
between the VS web server and IIS.

Specifically, the URL that I need to send when testing locally (VS
built-in web server) is http://localhost:4323/Safely-Sent/Validate.aspx.
However, in IIS, I need http://Safely-Sent.com/Validate.aspx.

So, I'd like to have a magic function that returns the correct 'base' url
(http://localhost:4323/Safely-Sent/ or http://Safely-Sent.com) for my
running instance as appropriate, onto which I can append my page name. I
looked at Request object properties, and at UriBuilder, but nothing looked
like what's needed.

Is there something trivial that I'm overlooking?

Thanks in advance!

May 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bill Murray | last post: by
11 posts views Thread by George Styles | last post: by
3 posts views Thread by Massimiliano Alberti | last post: by
5 posts views Thread by Mike Labosh | last post: by
6 posts views Thread by roland.bali | last post: by
7 posts views Thread by mrstephengross | last post: by
reply views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post: by
reply views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.