我有一个带有Entity Framework的MVC4应用程序。
我想要删除一个文件,但每次都会显示:
引发了类型为“System.UnauthorizedAccessException”的异常,但在用户代码中未被处理
附加信息:拒绝访问路径“G:\ Mijn Documents \ My Web Sites \ Lolabikes-Copy \ C#\ ContosoUniversity \ Images \”。
出错的代码是:System.IO.File.Delete(path);
这是方法:
public ActionResult DeleteFiles(int id)
{
//var fileName = Path.GetFileName(id.FileName);
var DirSeparator = Path.DirectorySeparatorChar;
var path = Server.MapPath("~\\Images" + DirSeparator);// + fileName.Replace('+', '_')));
var file = db.lolabikerPhotos.Find(id);
System.IO.File.Delete(path);
db.SaveChanges();
return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
}
我只是在Visual Studio中运行了这个应用程序,就像这样:http://localhost:41787/Account/Edit?UserId=hallo
我已经完成了以下工作:
对地图进行了完全访问,我将网络服务添加到具有完全控制权限的地图中。但似乎没有任何作用。我使用的是Windows 7,并且以管理员身份运行Visual Studio 2013。
我还看到了这个:
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
您可以在这里查看访问权限:
我尝试类似于这样的东西:
<system.web>
<identity impersonate="true" userName="Administrator" password="windowsPassword"/>
<httpRuntime requestValidationMode="2.0" maxRequestLength="1048576" executionTimeout="3600" />
<compilation debug="true" targetFramework="4.5" />
<pages validateRequest="false" />
<!--<httpRuntime targetFramework="4.5" />-->
</system.web>
我已经添加了这个:
<identity impersonate="true" userName="Administrator" password="windowsPassword"/>
好的,我可以运行该应用程序,但仍会出现错误:
Access to the path 'G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\' is denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.UnauthorizedAccessException: Access to the path 'G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\' is denied.
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
Source Error:
Line 489: var path = Server.MapPath("~\\Images" + DirSeparator);// + fileName.Replace('+', '_')));
Line 490: var file = db.lolabikerPhotos.Find(id);
Line 491: System.IO.File.Delete(path);
Line 492: db.SaveChanges();
Line 493:
完全权限:
高级选项卡:
已更改权限选项卡:
我将我的操作方法编辑成这样:
public ActionResult DeleteFiles(int id)
{
var fileName = Path.GetFileName(@"\\Koala.jpg");
var DirSeparator = Path.DirectorySeparatorChar;
var path = Server.MapPath(@"\\Images" + DirSeparator + fileName.Replace('+', '_'));
var file = db.lolabikerPhotos.Find(id);
LolaBikePhoto lola = db.lolabikerPhotos.Find(id);
db.lolabikerPhotos.Remove(lola);
System.IO.File.Delete(path);
db.SaveChanges();
return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
}
现在它正在运作!