500内部服务器错误 - 处理程序,asp.net MVC

时间:2013-01-19 06:01:02

标签: c# asp.net-mvc handler

我使用一个简单的处理程序在MVC视图上显示图像,它在我的localhost上工作得很好但是当我将它发布到我们的托管服务器时,我得到了这个错误(使用Fiddler):

GET ImageHandler.ashx?img=b...w=255&level=128&slice=0  500 Internal Server Error

以下是视图脚本:

<img id="Image1" alt="image"  src="ImageHandler.ashx?img=bmptest.bmp&window=255&level=128&slice=0" height="512" width="512" />

这是处理程序的脚本:

   public class ImageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string sImageFileName = "";
            int slice = 0;
            int window = 0;
            int level = 0;
            sImageFileName = context.Request.QueryString["img"].ToLower().Trim();
           .....
            System.Drawing.Image objImage = null;
            Image bmp = Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("content/" + sImageFileName));
            MemoryStream objMemoryStream = new MemoryStream();
            bmp.Save(objMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] imageContent = new byte[objMemoryStream.Length];
            objMemoryStream.Position = 0;
            objMemoryStream.Read(imageContent, 0, (int)objMemoryStream.Length);
            context.Response.ContentType = "image/jpeg";
            context.Response.BinaryWrite(imageContent);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

我通过添加:

修改了web.cong
<system.web>
......
    <httpHandlers>
      <add verb="*" path="*.ashx" type="DirectshowTest.ImageHandler,DirectshowTest"/>
    </httpHandlers>
   </system.web>

非常感谢您的建议。

1 个答案:

答案 0 :(得分:0)

我在Global.asax中添加了以下内容:

 routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");

现在正在运作。