|DataDirectory| returns wrong path to AppData folder

馋奶兔 提交于 2019-12-08 20:41:38

问题


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


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/18706945/datadirectory-returns-wrong-path-to-appdata-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!