命名空间'<global namespace =“”>'已包含'Handler“</global>的定义

时间:2014-04-10 08:32:57

标签: c# asp.net

此HTml代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Capctha Image with Refresh Button</title>
    <script type="text/javascript">
        function RefreshCaptcha() {
            var img = document.getElementById("imgcaptcha");
            img.src = "Handler.ashx?query=" + Math.random();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <img src="Handler.ashx" id="imgcaptcha" />
    <a href="#" onclick="javascript:RefreshCaptcha();">Refresh</a>
    </form>
</body>
</html>>

Captcha.aspx.cs

using System.Drawing;
using System.Drawing.Imaging;

public class Handler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        using (Bitmap b = new Bitmap(200, 50, PixelFormat.Format32bppArgb))
        {
            using (Graphics g = Graphics.FromImage(b))
            {
                Rectangle rect = new Rectangle(0, 0, 150, 40);
                g.FillRectangle(Brushes.White, rect);

                //Creating String Draw
                Random r = new Random();
                int StartIndex = r.Next(1, 5);
                int Length = r.Next(5, 15);
                string DrawString = Guid.NewGuid().ToString().Replace("-", "0").Substring(StartIndex, Length);


                //Creating Fount and Bursh
                Font drawFont = new Font("Arial", 16, FontStyle.Italic | FontStyle.Strikeout);

                using (SolidBrush drawBrush = new SolidBrush(Color.Blue))
                {
                    // Create point for upper-left corner of drawing.
                    PointF pointdraw = new PointF(10, 15);
                    // Draw string to screen.
                    g.DrawRectangle(new Pen(Color.Blue, 0), rect);
                    g.DrawString(DrawString, drawFont, drawBrush, pointdraw);
                }

                b.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                context.Response.ContentType = "image/jpeg";
                context.Response.End();
            }
        }
    }

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

我正在获取Erros名称空间&#39;&#39;已包含&#39; Handler&#34;的定义像这样如何解决它并对错误给出任何想法 我正在添加handler.ashx

1 个答案:

答案 0 :(得分:0)

您是在编译时还是运行时遇到此错误?

此错误通常发生在编译时,并表明还有另一个具有类名Handler的Page / Handler / UserControl / ServerControl。

同时看看你的结构,事情真的很有意义:

  • Captcha.aspx拥有HTML代码(但我确实相信,通过指令指向Captcha.aspx.cs
  • Captcha.aspx.cs包含课程Handler(实现System.Web.IHttpHandler,但应该是System.Web.UI.Page

总结一下:我相信引用存在缺陷。 Captcha.aspx.cs的内容错误,可能与Handler.ashx.cs - 文件...

重复