views:

214

answers:

3

I'm trying to implement a server control that frobs a couple of files inside the web directory of an ASP.NET site. I'm using VS Web Dev Express 2008 as my IDE. When I call HttpContext.Current.Request.ApplicationPath to get a path to the web root so I can find those files, it returns C:. What the heck?

Absolute paths work just fine, but I'd like to be able to feed the server control a relative directory and just let it do it's thing. What have I done wrong?

public String Target
{
    get { return _target; }
    set
    {
        if (value.StartsWith("~"))
        {
            // WTF? Gives me C:\? Why?
            _target = HttpContext.Current.Request.ApplicationPath +
                value.Substring(1);
        }
        else
        {
            _target = value;
        }
    }
}

private String _target;

protected override void Render(HtmlTextWriter writer)
{
    HtmlControl wrapper = new HtmlGenericControl("div");
    int fileCount = 0;

    try
    {
        DirectoryInfo dir = new DirectoryInfo(_target);
        foreach (FileInfo f in dir.GetFiles())
        {
            fileCount++;
            a = new HtmlAnchor();
            a.Attributes.Add("href", f.FullName);
            a.InnerHtml = f.Name;
            wrapper.Controls.Add(a);
        }
    }
    catch (IOException e)
    {
        throw e;
    }

    Controls.Add(wrapper);
    base.Render(writer);
}
+3  A: 

This might be because it's using the development web server, which can just serve files from any directory on your hard drive. It doesn't have any specific root. Can you run your project under IIS (assuming your version of windows supports it), and see if you get the same results?

To get rid of the problem completely you could just hard code the path you want to look at in your web.config and go around any problems with what Request.ApplicationPath is returning.

[EDIT]

Just found out you can use

HTTPContext.Current.Request.ServerVariables("APPL_PHYSICAL_PATH")

to return the path of your application, on the hard disk. I'm pretty sure that's what you are looking for. If that's not right, check out all the other ServerVariables to see if you can get what you are looking for.

Kibbee
No, I happen to be hamstrung with XP Home at the moment. No IIS.I may end up hardcoding that directory eventually, but the whole point is for the control's user to be able to specify the dir at design time.
Dustman
Yeah, it must be the mini server that VS provides... but you'd think it would know where it's getting the files from.
Dustman
+2  A: 

How about this:

Server.MapPath(ResolveUrl("~/filename"))

There's also information on a page TLAnews.com titled, Understanding Paths in ASP.NET.

Mark Cidade
That uses the same web root directory, so I'll have the same problem.
Dustman
The server must know the real directory somehow, though. Try breaking in the debugger and inspecting every path-related property in the HttpRequest object.
Mark Cidade
A: 

The ADME Developer's Kit may be what you need if you are trying to get the directory at design time.

Timothy Lee Russell