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.
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.
Member
5 Points
34 Posts
Virtual Path vs Relative Path
Aug 26, 2019 09:16 PM|learning_to_code|LINK
Hello,
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"))
All-Star
57884 Points
15513 Posts
Re: Virtual Path vs Relative Path
Aug 26, 2019 11:57 PM|bruce (sqlwork.com)|LINK
Member
5 Points
34 Posts
Re: Virtual Path vs Relative Path
Aug 27, 2019 06:43 PM|learning_to_code|LINK
So what happens when I deploy this to Azure? How will the path resolve, if a relative path doesn't work?
All-Star
52261 Points
23316 Posts
Re: Virtual Path vs Relative Path
Aug 27, 2019 06:58 PM|mgebhard|LINK
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.