Server.MapPath to go two folder back from root

后端 未结 5 1127
鱼传尺愫
鱼传尺愫 2020-12-30 12:12

Here is how I do it:

HttpContext.Current.Server.MapPath(@\"~\\~\\~\\Content\\\")

HI know that \'.\' is for the root of the project, but how

5条回答
  •  猫巷女王i
    2020-12-30 12:36

    Since you are using MapPath you are getting returned a physical path(\) from a virtual path(/).

    Creating a DirectoryInfo object or using a Path utility method starting from your child app root won't necessarily give you what you expect unless your virtual parent and virtual grandparent have the same hierarchy as your physical directory structure.

    My apps are not physically nested to match the Url depth. This could also be the case if a virtual directory is involved.

    Assuming a grandparent app is two virtual folders up this would get you the physical path...

        string physicalGrandparentPath = HttpContext.Current.Server.MapPath("~/../../");
    

    Using this would keep you safe from any virtual directory shuffle games going on in the IIS setup.

    I used this to see how far I could go up. I did not get an HttpException until I tried to go above wwwroot.

提交回复
热议问题