Virtual Path vs Relative Path RRS feed

  • Question

  • User1266884534 posted


    I have a beginner question on the physical path of a file.

    The files are in the app_Data folder. The first line of code works AND that path is fully resolved.

    How can I have a relative path that will work?

    FULL PHYSICAL PATH (Resolves and loads the object correctly).

    JsonSource = new UriJsonSource(new Uri(@"C:\source\repos\DevExtremeWebApp\DevXWebDoc\App_Data\FirstInvHeaderwithDetail.json"))

    RELATIVE PATH 1 (Doesn't resolve and throws a runtime error.

    JsonSource = new UriJsonSource(new Uri(@"\App_Data\SecondInvHeaderwithDetail.json"))

    RELATIVE PATH 2 (Doesn't resolve and throws a runtime error. Notice the ~ sign.

    JsonSource = new UriJsonSource(new Uri(@"~\App_Data\SecondInvHeaderwithDetail.json"))

    Monday, August 26, 2019 9:16 PM

All replies

  • User-474980206 posted
    At runtime the default working folder, is where the asp.net worker process (not your app) is running from, so relative paths don’t work. Normally if you want a path relative to your apps vdir than you would use Server.MapPath.
    Monday, August 26, 2019 11:57 PM
  • User1266884534 posted

    So what happens when I deploy this to Azure? How will the path resolve, if a relative path doesn't work?

    Tuesday, August 27, 2019 6:43 PM
  • User475983607 posted

    I'm not sure where you are getting the relative path doesn't work.  Your code is trying to access a physical path.  The physical path of your web application files can be stored anywhere on the physical media.  When you configure your hosted app, you tell the host where to find the physical applcation files on disk.

    To get the physical path, the syntax is...

    string path = HttpContext.Current.Server.MapPath("~/App_Data/FirstInvHeaderwithDetail.json");

    The tilde resolves to the hosted application root to the physical location.  This approach will work everywhere in your app.

    Tuesday, August 27, 2019 6:58 PM