|DataDirectory| returns wrong path to AppData folder
Asked Answered
E

3

8

|DataDirectory|, you had one job. |DataDirectory| on IIS7.5 resolves to:

C:\inetpub\wwwroot\appname\App_Data

That folder does not exist. The correct folder is:

C:\inetpub\wwwroot\appname\bin\App_Data

All is well on the dev machine, but when I deploy to the server, AppData is placed within bin\. I use AppData as shown in any Visual Studio 2010 project and deploy with "Build Deployment Package" (VS2010) then "Import Application" (IIS Manager).

I know I can set the path manually with AppDomain.SetData or similar, but my interpretation of the point of the feature is to return the correct location for deployment on different servers. It isn't very useful if I have to hard code that path.

The connection string: "Data Source=|DataDirectory|\db.sqlite;"

How do I make |DataDirectory| return the AppData path or alternatively prevent creation of the unnecessary bin\ folder?

Ellersick answered 9/9, 2013 at 21:0 Comment(2)
I hope you mean @"Data Source=|DataDirectory|\db.sqlite;", otherwise it is going to look for a file named App_Datadb.sqlite.Dysprosium
I tried both, and both actually work. The resolved directory in the question is a copy & paste from <h1>@AppDomain.CurrentDomain.GetData("DataDirectory")</h1> (which strangely wasn't documented).Ellersick
S
4

It is broken for me too. I tried various combinations of "Copy / Do Not Copy" and changing the "ExcludeApp_Data" parameter but could not get it consistent between Visual Studio and the published version. To avoid having two copies of the data on the server, I ended up changing the application to check the App_Data folder first and if not found to look in the bin/App_Data folder.

Serrulate answered 11/1, 2014 at 3:12 Comment(1)
like wise, am having similar issue accessing the .cer file from app_data in my web api. using a blob storage could be a choice, but i am going to use the same method of alternating between both the locations. not a very clean approach, but that seems to be a necessary evil,Kiersten
C
2

One approach is to NOT hard-code the path in your C# source, but to put it in your web.config. Then you can test with one path with your local VisualStudio web server, and use a modified path for production with IIS.

The added benefit is documentation of your App_Data tree, e.g.:

<appSettings>    
  <add key="ProjABCstyledir" value="~/bin/App_Data/ABCstyles" />
  ...
</appSettings>

In your code you should use Server.MapPath() to resolve the tilde.

Compassionate answered 21/12, 2015 at 13:37 Comment(0)
F
1

In Visual Studio, make sure the Properties are set correctly for every file in the App_Data folder:

enter image description here

If there are any files with Build Action = Content, this will create a appname\App_Data folder during the build with a copy of the file inside it.

Copy to Output Directory = Do not copy, prevents a copy of the file being placed in appname\bin\App_Data. If all files are set to Do not copy, then the folder won't be created during the build.

Foxtrot answered 9/12, 2020 at 16:23 Comment(1)
I took over a project from someone and they added the file as an "embedded resource". Re-adding the file as Content and Do Not Copy resolved the issue for me.Kablesh

© 2022 - 2024 — McMap. All rights reserved.