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 asp.net 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?
Js
LVL 2
jshesekAsked:
Who is Participating?
 
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
<configuration>
  <appSettings>
    <add key="AppOne" value="http://localhost/web1/" />
    <add key="AppTwo" value="http://localhost/web2/" />
 </appSettings>
</configuration>

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
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
are you using the ~ ?

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

Response.Redirect("~/news/newspage.aspx");
0
 
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.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
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.
0
 
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
0
 
jshesekAuthor Commented:
Oh my gosh!!!!  Thanks a bunch.  It works like a charm.  This will save me so much time.

Excellent answer.
0
 
jshesekAuthor Commented:
Perfect Answer.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.