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

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
 
S

sloan

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 "~/";

}



}
}
 
S

SpaceMarine

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
 
S

SpaceMarine

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;
}
 
S

sloan

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

sloan

"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.
 
B

bruce barker

simple, use the Control method:

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

-- bruce (sqlwork.com)
 
S

SpaceMarine

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
 
A

Andrew Morton

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
 
S

SpaceMarine

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top