Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

|DataDirectory| returns wrong path to AppData folder

|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?

like image 625
Charles Burns Avatar asked Sep 09 '13 21:09

Charles Burns


2 Answers

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.

like image 153
adg Avatar answered Sep 20 '22 05:09

adg


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.

like image 36
Roland Avatar answered Sep 24 '22 05:09

Roland