我有两种方法来检索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句子上。
这是有时显示的错误消息:
"No images found for record with GUID: 8689ffcd-46d2-4a25-ab57-edce11e45977 and Image Mask Name: 002475-20190702_070949_326".
谢谢您的帮助。
答案 0 :(得分:0)
我做了一些测试,它是由以下代码引起的:
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”的值设置为项目根目录并将相对于该路径的路径存储在静态变量中的问题。