Thursday, March 22, 2012

opposite of MapPath

What method is the opposite of MapPath?

On an ASPX page's code-behind, I know that I can can do this...

this.MapPath("test.htm");

...and get back something like this...

c:\mydirectory\test.htm

...but what need to do is call something like this...

this.OppositeOfMapPath("c:\mydirectory\test.htm");

...and get back something like this...

http://www.mysite.com/test.htm

...and so on.

How can I do that?

Please advise.

Thank you.

-- Mark Kamoski

I suppose some basic string maniplulation could accomplish this task:

Dim strRootDir asString ="MyDirectory"'Probably will be Visual Studio project nameDim strRealUrl asString ="http://www.mysite.com"Dim strThis asString = Server.MapPath("test.htm")Dim strThat asString If strThis.IndexOf(strRootDir) = -1Then strThat ="DIR NOT FOUND IN PATH!"Else strThat = strRealUrl &"/" & strThis.Substring(strThis.IndexOf(strRootDir) + 1).Replace("\","/")End If

Of course you can turn this into a function that would return strThat (and possibly accept variables like strTheRealUrl and strRootDir) and then it would be as easily accessible as Server.MapPath()

Hope this helps! Don't forget to mark the most helpful post(s) asAnswer for the sake of future readers. Thanks!


Is there another way to do that? Does ASP.NET provide a utility to do that?

The virtual path is decided at the IIS virtual path property window by the IIS admin. It may be called "MyDirectory_UAT" since there is already a "MyDirectory" for development purpose there. This is only know at the runtime. At compile time, you do not know what virtual path will be given.

Thanks!

0 comments:

Post a Comment