Using "~" in a path is permitted as C: \

I am trying to implement a server control that breaks a couple of files inside an ASP.NET site web directory. I am using VS Web Dev Express 2008 as my IDE. When I call HttpContext.Current.Request.ApplicationPathto get the path to the root website, so I can find these files, it returns C :. What the hell?

Absolute paths work fine, but I want me to be able to manage the server with a relative directory and just let it do that. What did I do 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
source share
3 answers

, , - , . . IIS ( , ), , ?

, web.config - , Request.ApplicationPath.

[EDIT]

,

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

. , . , ServerVariables, , , .

+3

:

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

TLAnews.com ASP.NET.

+2

The ADME Developer Kit may be what you need if you are trying to get a catalog during development.

-1
source

Source: https://habr.com/ru/post/1697893/


All Articles