【问题标题】:MVC 3: CustomResult() doesn't find suitable method to overrideMVC 3:CustomResult() 找不到合适的方法来覆盖
【发布时间】:2012-01-03 12:44:13
【问题描述】:

我正在创建一个图像结果.. 但是“公共覆盖 void ExecuteResult (ControllerContext Context)”说:没有找到合适的方法来覆盖。

我的班级看起来像这样..

    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();

    }

【问题讨论】:

    标签: image asp.net-mvc-3 overriding


    【解决方案1】:

    你的类是ActionResult 的子类吗?请尝试以下操作:

    using System.Web.Mvc;
    
    public class ImageResult : ActionResult 
    {
      public override void ExecuteResult(ControllerContext Context)
      {
        ...
      }
    }
    

    【讨论】:

    • @Freetalk13 覆盖是 .Net 的功能,而不是 MVC。它适用于所有 C# 版本。
    • @Freetalk13 你有对 System.Web.Mvc.dll 的引用吗?
    • 是的,我有.. 关闭并打开项目后,它没有显示任何错误。谢谢。但是当我转到'localhost:3761/home/getimage'时,它会显示一个服务器错误--> C:\{pathToImage}.jpg 并抛出 new FileNotFoundException(_path);我怎样才能通过路径?
    • @Freetalk13 我假设 _path 是 ImageResult 类上的一个字段,在实例化 ImageResult 时传递它(可能使用 HttpServerUtility.MapPath 方法)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多