Nick Reeve
Nick Reeve

Reputation: 1680

ASP.NET MVC 4 Access to the path App_Data is denied

I have been getting the following error after publishing my site:

System.UnauthorizedAccessExceptionAccess to the path 'C:\inetpub\MySite\App_Data' is denied.

Turns out it's because it can't access App_Data\ASPNETDB.MDF. This is because it doesn't exist as my site doesn't use it. I checked my local machine and there is an App_Data folder with the database in but it isn't included in my build in VS. If I delete it however, it is recreated when I run the site in VS.

The site works fine after that once the error seems to clear itself but it happens every time I deploy.

There is no reference to it anywhere in code. How/Why is it getting created on application start and how do I stop it?

I am using SimpleMembership with all data stored in a SQL Server DB.

Upvotes: 1

Views: 8308

Answers (2)

Masoud Ghaffari
Masoud Ghaffari

Reputation: 61

I had this problem before. when you want to publish your App, if the app_data folder was empty, it doesn't copy to the published one. so before publishing, copy a file to app_data folder, then publish your app... or you can check for exist to create inside the code:

var folder = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/");
if (!Directory.Exists(folder)) 
    Directory.CreateDirectory(folder);

Upvotes: 2

Avorsa
Avorsa

Reputation: 1

Try to go to App_Data folder property and add ASPNET user with read and write privileges

Access to the path 'c:\inetpub\wwwroot\myapp\App_Data' is denied

Upvotes: 0

Related Questions