asp.net-mvc - 随机文件访问问题 IIS
问题描述
我有两种方法可以在 ASP.NET MVC 网站上检索图像。如果我使用文件资源管理器搜索磁盘上的图像,图像就在那里,但我的日志文件显示许多错误,表明每个图像都不存在。
有时它可以正常工作,但在其他情况下,我的应用程序找不到图像。如果我重新启动我的 IIS 站点或重新启动计算机,它可以正常工作,但它具有我必须解决的随机行为。
图像所在的两条路径是:
"C:\inetpub\wwwroot\Test_Application\Content\Images" (Inside site public dir)
"C:\Test_Application\images" (Configured on IIS as a virtual directory)
这是一个运行 IIS 的 Windows 10 Pro 设备,并且运行该站点的应用程序池具有一个具有管理权限的 Windows 帐户作为身份,因为我需要连接到 SQL Server LocalDb。
将消息写入日志的函数是:
private bool Func_GetImages(RecordModel record)
{
bool bResult = false;
List<string> imagesFound = new List<string>();
string sImageMaskName = string.Empty;
string sAbsoluteImgPath = string.Empty;
string sSourceImgPath = string.Empty; //Path to img directory with year, month and day.
string sTargetImgPath = string.Empty;
try
{
Config.LogHelper.Func_WriteEventInLogFile(DateTime.Now.ToLocalTime(), enum_EventTypes.Debug, "Func_GetImages", "Get Images: ",
string.Format("Get images for record with GUID: {0}; Image Mask Name: {1}; Times for Images: {2}", record.GUID, record.imageMaskName,
record.TimeBetweenColorImages));
if (HttpContext.Current != null)
{
sAbsoluteImgPath = HttpContext.Current.Server.MapPath(Config.sImgDirectory);
sTargetImgPath = HttpContext.Current.Server.MapPath("~/Content/Images/Processed");
}
else
{
sAbsoluteImgPath = string.Format("{0}/", AppDomain.CurrentDomain.BaseDirectory);
sTargetImgPath = string.Format("{0}/", "~/Content/Images/Processed");
}
sSourceImgPath = Path.Combine(sAbsoluteImgPath, record.DetectionDatetime.Year.ToString(), record.DetectionDatetime.Month.ToString("D2"), record.DetectionDatetime.Day.ToString("D2"));
Config.LogHelper.Func_WriteEventInLogFile(DateTime.Now.ToLocalTime(), enum_EventTypes.Debug, "Func_GetImages", "Get Images: ",
string.Format("Path to search for files: {0}", sSourceImgPath));
sImageMaskName = record.imageMaskName;
if (!Directory.Exists(sSourceImgPath))
{
Directory.CreateDirectory(sSourceImgPath);
}
//Try to get files with this search pattern
imagesFound = Directory.GetFiles(sSourceImgPath, string.Format("{0}*", sImageMaskName)).ToList();
if (imagesFound.Count > 0)
{
Config.LogHelper.Func_WriteEventInLogFile(DateTime.Now.ToLocalTime(), enum_EventTypes.Debug, "Func_GetImages", "Get Images: Ok",
string.Format("Number of images for record with GUID: {0} is {1}", record.GUID, imagesFound.Count.ToString()));
//Create new image name
DateTime dateCapture = record.DetectionDatetime;
string sImageName = record.imageMaskName;
//Check if day directory exists. If not, create it
Func_CheckOrCreateImageDirectory(dateCapture);
string sBaseDirectory = Path.Combine(sTargetImgPath, dateCapture.Year.ToString(), dateCapture.Month.ToString("D2"), dateCapture.Day.ToString("D2"));
//Rename, add image header and copy blackWhiteImages
Func_ProcessImages(sBaseDirectory, sImageMaskName, sImageName, detection.numPicturesColor, enum_ImagesType.COLOR, detection);
}
else
{
Config.LogHelper.Func_WriteEventInLogFile(DateTime.Now.ToLocalTime(), enum_EventTypes.Error, "Func_GetImages", "Get Images: Ko",
string.Format("No images found for record with GUID: {0} and Image Mask Name: {1}", record.GUID, sImageMaskName));
}
bResult = true;
}
catch (Exception exception1)
{
Config.LogHelper.Func_WriteEventInLogFile(DateTime.Now.ToLocalTime(), enum_EventTypes.Error, new StackTrace(exception1).GetFrame(0).GetMethod().Name, "GeneralException",
"Source = " + exception1.Source.Replace("'", "''") + ", Message = " + exception1.Message.Replace("'", "''"));
}
return bResult;
}
当我检查“imagesFound.Count > 0”时,出现问题时写入的日志消息位于 else 语句中。
这是有时会出现的错误消息:
找不到用于 GUID 的记录的图像:8689ffcd-46d2-4a25-ab57-edce11e45977 和图像掩码名称:002475-20190702_070949_326
谢谢您的帮助。
解决方案
我做了一些测试,它是由这段代码引起的:
if (HttpContext.Current != null)
{
sAbsoluteImgPath = HttpContext.Current.Server.MapPath(Config.sImgDirectory);
sTargetImgPath = HttpContext.Current.Server.MapPath("~/Content/Images/Processed");
}
else
{
sAbsoluteImgPath = string.Format("{0}/", AppDomain.CurrentDomain.BaseDirectory);
sTargetImgPath = string.Format("{0}/", "~/Content/Images/Processed");
}
我解决了将“Web.config”中“sImgDirectory”的值设置为项目根目录并将相对于该路径的路径存储在静态变量中的问题。
推荐阅读
- angular - 如何嵌入没有 CORS 问题的 Angular 应用程序
- django - 为包含对象的 django 模型提供服务
- reactjs - 我如何使用 Promise 对象
- python - 把我的for循环变成python中的一个函数
- c# - Viewbag 没有将回发的值绑定到控制器?
- spring-boot - 如何在 Spring Boot 应用程序中为嵌入式 tomcat 启用 jmx
- python - 刷新的对象是从数据库事务缓冲区返回还是从 SQLAlchemy 中的内存表示返回?
- php - Laravel 应用程序的配置何时初始化?或者当 DB::connection 设置时?
- linux - 预期值:CLOSED,实际值:“CLOSED”
- r - 传递向量变量时 sum 函数不起作用