helper function - full app path ("~/")

  • Thread starter Thread starter SpaceMarine
  • Start date Start date
S

SpaceMarine

hello,

i find the server-side "~/" shortcut incredibly helpful for setting
properties on some of my server controls. it finds the full
application path to the specified URL.

is there a way to perform a similar application path in code-behind?

Server.MapPath is not it, that finds the full path from the file
system.

im looking for the full app path -- which may or may not be a virtual
folder... ex: when using VS.NET there is no app sub-folder name, since
the built-in VS web server runs the app as root. however when deployed
to production, my code runs under a virtual-folder app name.

is there a nice tidy way to retrieve this full app path?


thanks!!
sm
 
You can EXPERIMENT with these. Judging from the commented out sections...I
had the same issues at some point.



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MyNameSpace
{
public class DirectoryHelpers
{


public static string FindPhysicalRootDirectory(Page p)
{

string rootDir;
// rootDir = p.Server.MapPath("/");

rootDir = p.Server.MapPath("~/");

return rootDir;

}

public static string FindVirtualRootDirectory(Page p)
{


//Dim loop1, loop2 As Integer
//Dim arr1(), arr2() As String
//Dim coll As NameValueCollection

//' Load ServerVariable collection into NameValueCollection
object.
//coll = p.Request.ServerVariables
//' Get names of all keys into a string array.
//arr1 = coll.AllKeys
//For loop1 = 0 To arr1.GetUpperBound(0)
// p.Response.Write("Key: " & arr1(loop1) & "<br>")
// arr2 = coll.GetValues(loop1) ' Get all values under this key.
// For loop2 = 0 To arr2.GetUpperBound(0)
// p.Response.Write("Value " & CStr(loop2) & ": " &
p.Server.HtmlEncode(arr2(loop2)) & "<br>")
// Next loop2
//Next loop1



return "~/";

}



}
}
 
is there a nice tidy way to retrieve this full app path?

i found this helpful:

string appFolderName = HttpRuntime.AppDomainAppVirtualPath; // "/" in
VS or "/EprWhatever" in production


....using that you can write a function to take in a path and prefix
it.


sm
 
this is what ive got:

public static string GetUrlsAppPath(HttpContext context, String url)
{
string returnValue;

if (url.StartsWith("~"))
{
returnValue = url.Substring(1);

if (context.Request.ApplicationPath != "/")
returnValue = context.Request.ApplicationPath + returnValue.Replace
("//", "/");
}
else
{
returnValue = url;
}

return returnValue;
}
 
I'll look at your as well (tomorrow?)

Make sure you test it with

IIS as your development web server.
Cassinni (or the VS2008 equivalent) "built in" webserver.

That's where I find the most issues. I use IIS locally, but some developers
use the built in one of course.

It should let you pick and alternate under the Web properties for the csproj
or vbproj file.
 
"your code" that is


sloan said:
I'll look at your as well (tomorrow?)

Make sure you test it with

IIS as your development web server.
Cassinni (or the VS2008 equivalent) "built in" webserver.

That's where I find the most issues. I use IIS locally, but some
developers use the built in one of course.

It should let you pick and alternate under the Web properties for the
csproj or vbproj file.
 
simple, use the Control method:

var path = ResolveClientUrl("~/mypath");

-- bruce (sqlwork.com)
 
bruce,

does this require instantiating a Control object in order to call?
prefer not to create a Control object if a helper function can do it
cheaper.


sm
 
does this require instantiating a Control object in order to call?
prefer not to create a Control object if a helper function can do it
cheaper.

You can use the current Page as that control rather than instantiating some
new control.

Andrew
 
You can use the current Page as that control rather than instantiating some
new control.

....thats not a bad idea. only thing is if this functionality is placed
into a non-page portion of the app code; then id need to pass a weight
Page object into it.

i modified my previous function to lighten it up futher:

public static string GetAppPathUrl(string appPath, String url)
{
string returnValue;

if (url.StartsWith("~"))
{
returnValue = url.Substring(1);

if (appPath != "/")
returnValue = appPath + returnValue.Replace("//", "/");
}
else
{
returnValue = url;
}

return returnValue;
}

....not married to it, but seems cheaper than passing Page objects
around.


sm
 
Back
Top