Find Base URL Of My Web App?

A

Alan Cohen

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!
 
A

Alan Cohen

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.
 
K

Kevin Spencer

You may want to look at the System.Web.VirtualPathUtility class
(http://msdn2.microsoft.com/en-us/library/system.web.virtualpathutility.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
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top