Get Virtual Directory URL

Hi Experts,

I set up a virtual directory in IIS 7.  It points to a different web.  I'm trying to create a web link - to go from current web to virtual directory web.  The reason for this is there are 3 different servers I have to move the pages to and I don't want to hand code links in each server.  Using 4.0.

So Ex:  There's web1 and web2.  In web2 there's a virtual directory, called "mainweb1" pointing to web1.  
In web2 there's a link that I want to go to web1 using the virtual directory.  

What the link should look like in web2: http://localhost/web1/news/newspage.aspx
Currently it comes out like:  http://localhost/web2/web1/news/newspage.aspx

Any ideas?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
are you using the ~ ?

~ says always go to the root of the application . . .  so your link should look something like

jshesekAuthor Commented:
Hi Kyle,
Yes actually I've used:
Dim thisServer as String = VirtualPathUtility.ToAbsolute("~/Web1/")
Dim thisServer As String = Request.ApplicationPath("~/Web1/")
Dim thisServer As String = FilePath.GetPathRoot("~/Web1/")
Dim thisServer As String = FilePath.GetFullPath("~/Web1/")
No luck.  
This goes into a hyperlink where the user can choose to go there or not.
Kyle AbrahamsSenior .Net DeveloperCommented:
Don't use /Web1/

just use "~/News/"

~ returns the application root . . . in your case localhost/Web1 or localhost/web2  you want everything underneath that.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

jshesekAuthor Commented:
Sorry to be so dense....

The link is on this page: http://localhost/web2/active.aspx
I need it to go to here: http://localhost/web1/newsPage.aspx
The virtual directory is on IIS for web2 called mainweb1.
web1 and web2 are different domains.

If the coding is:

Function GetURL() as string
   Dim thisServer as string = "~/mainweb1/"
   Dim thisURL as string = thisServer & "newsPage.aspx"
   Return thisURL
End Function

<asp:HyperLink ID="lnkURL" runat="server" NavigateUrl='<%# GetURL() %>' target="_blank" Text="link to news"></asp:HyperLink>

The URL in the link turns out to be:  http://localhost/web2/mainweb1/newPage.aspx
should be http://localhost/web1/newPage.aspx
Kyle AbrahamsSenior .Net DeveloperCommented:
Didn't realize you were trying to redirect across applications.

In that case I would make appSettings in your web.config
    <add key="AppOne" value="http://localhost/web1/" />
    <add key="AppTwo" value="http://localhost/web2/" />

That can then be changed per application.  You can even move it

'Imports System.Configuration
Function GetURL() as string
   Return ConfigurationManager.AppSettings("AppOne").ToString() + "NewPage.aspx"
End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jshesekAuthor Commented:
Oh my gosh!!!!  Thanks a bunch.  It works like a charm.  This will save me so much time.

Excellent answer.
jshesekAuthor Commented:
Perfect Answer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.