Image MVC 3:CustomResult()未找到合适的方法进行重写
我正在创建一个imageresult。。但是public override void executesult ControllerContext上下文说:找不到合适的方法来override 我的班级看起来像这样Image MVC 3:CustomResult()未找到合适的方法进行重写,image,asp.net-mvc-3,overriding,Image,Asp.net Mvc 3,Overriding,我正在创建一个imageresult。。但是public override void executesult ControllerContext上下文说:找不到合适的方法来override 我的班级看起来像这样 public override void ExecuteResult(ControllerContext Context) { byte[] bytes; string contentType = GetContentTypeFromFil
public override void ExecuteResult(ControllerContext Context)
{
byte[] bytes;
string contentType = GetContentTypeFromFile();
//if there's no context, stop the processing
if (Context == null)
{
throw new ArgumentNullException("context");
}
//check for file
if(File.Exists(_path)){
bytes = File.ReadAllBytes(_path);
}
else{
throw new FileNotFoundException(_path);
}
//
HttpResponseBase response = Context.HttpContext.Response;
response.ContentType = contentType;
MemoryStream imageStream = new MemoryStream(bytes);
byte[] buffer = new byte[4096];
while (true)
{
int read = imageStream.Read(buffer, 0, buffer.Length);
if (read == 0)
break;
response.OutputStream.Write(buffer, 0, read);
}
response.End();
}
您的类是否为ActionResult子类?请尝试以下操作:
using System.Web.Mvc;
public class ImageResult : ActionResult
{
public override void ExecuteResult(ControllerContext Context)
{
...
}
}
@Freetalk13覆盖是.Net的一项功能,而不是MVC。它在C的所有版本中都可用。@Freetalk13您有System.Web.Mvc.dll的参考吗?是的,我有。。关闭并打开项目后,不会显示任何错误。谢谢但当我转到时,它会显示一个服务器错误->C:\{pathToImage}.jpg并抛出新的FileNotFoundException\u路径;如何传递路径?@Freetalk13我假设_path是ImageResult类上的一个字段,在实例化ImageResult时传递它,可能使用HttpServerUtility.MapPath方法。