[英]Directory not found error in WCF service call in IIS
我在C#中使用asp.net 3.5
在我的Web应用程序中,有一个名为SlideShow
的文件夹,其中包含图像。 我想获取具有相应URL的图像名称,并使用WCF以JSON格式返回它。 我创建了一个返回包含图像路径和图像数组的json字符串的方法。 当我使用ASP.NET开发服务器运行应用程序时,它运行良好,但在IIS上却显示错误。
我的界面是
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
Stream GetSlideShowImages();
服务方法是
Stream Iremoteclient.GetSlideShowImages()
{
byte[] resultBytes = Encoding.UTF8.GetBytes(new AndroidServices().GetSlideShowImages());
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
saveAndroidRequest();
return new MemoryStream(resultBytes);
}
}
和从中获取json字符串的方法是
public string GetSlideShowImages()
{
try
{
string relativepath = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;
string pathlastchr = relativepath.Trim().Substring(relativepath.Length - 1, 1);
if (pathlastchr == "/")
{
relativepath = relativepath.Trim() + "SlideShow/";
}
else
{
relativepath = relativepath.Trim() + "SlideShow/";
}
string json;
DirectoryInfo directoryInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath("/SlideShow"));
FileInfo[] file = directoryInfo.GetFiles().Where(f => f.Extension == (".bmp") || f.Extension == (".jpg") || f.Extension == (".png") || f.Extension == (".TIFF") || f.Extension == (".gif")).ToArray();
AndroidServices objAndroidServices = new AndroidServices();
objAndroidServices.ImagePath = relativepath;
objAndroidServices.Images = file.Select(f => f.Name).ToArray();
using (MemoryStream ms = new MemoryStream())
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(AndroidServices));
ser.WriteObject(ms, objAndroidServices);
json = System.Text.Encoding.UTF8.GetString(ms.GetBuffer(), 0, Convert.ToInt16(ms.Length));
json = json.Replace(@"\/", @"/");
}
return json;
}
catch (Exception ex)
{
return ex.Message;
}
}
在IIS上,它给出了类似的错误
“位于System.IO.Directory.InternalGetFileDirectoryNames(字符串路径,字符串userPathOriginal,字符串searchPattern,布尔值i处,位于System.IO .__ Error.WinIOError(Int32 errorCode,字符串mayFullPath)处”和“位于System.Text.Encoding.GetBytes(String s) ),位于E:\\ VivifyHealth \\ caregiverihportal \\ src \\ service \\ remoteclient.svc中的ihv1Role.service.remoteclient.ihv1Role.service.Iremoteclient.GetSlideShowImages()
有什么办法吗?
因此,问题在于您需要更改以下内容:
DirectoryInfo directoryInfo =
new DirectoryInfo(HttpContext.Current.Server.MapPath("/SlideShow"));
对此:
DirectoryInfo directoryInfo =
new DirectoryInfo(HttpContext.Current.Server.MapPath("~/SlideShow"));
根据MSDN文档:
...路径开头的斜杠(/)表示该站点的绝对虚拟路径。
但是,当您以~
开头时,它将从站点的根目录开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.