【问题标题】:How to handle exceptions in HTTPHandler?如何处理 HTTPHandler 中的异常?
【发布时间】:2018-02-20 03:24:37
【问题描述】:

我有使用 HTML 和 asp.net HTTP 处理程序的邮件发送程序。

这是我向 .ashx 页面发送请求的 html 页面

<html>
<form id="ContactForm" action="EmailHandler.ashx" method="post">
    Name: <input class="input" type="text" id="contactname" name="contactname" autocomplete="on" required/>
    Email <input class="input" type="email" id="contactemail" name="contactemail" autocomplete="on" required />
    Comments<textarea cols="50" rows="5" id="contactmessage" name="contactmessage" required></textarea>

    <input type="submit" id="emailsubmit" name="emailsubmit" value="Send" />
</form>
</html>

这是我的 httphandler (EmailHandler.ashx)

public class EmailHandler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        string name = context.Request.Form["contactname"];
        string email = context.Request.Form["contactemail"];
        string content = context.Request.Form["contactmessage"];
        try
        {
            SendEmail(name, email, content);

        }
        catch (Exception)
        {
           *******
        }
    }

    public bool IsReusable { get { return false; } }

    public void SendEmail(string name, string emailid, string emailcontent)
    {
        SmtpClient serverobj = new SmtpClient();
        MailMessage msgobj = new MailMessage();

        msgobj.To.Add("abc@ymail.com");
        msgobj.Subject = "Its From Guest";
        msgobj.Body = emailcontent;
        msgobj.IsBodyHtml = true;
        msgobj.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
        serverobj.Send(msgobj);

    }

现在如何处理来自这个处理程序的响应。

  1. 如果邮件发送成功,如何将成功信息反映到同一个html页面?

  2. 如果出现异常,如何处理异常或异常消息?

【问题讨论】:

标签: c# asp.net html http


【解决方案1】:

您可以使用context 将值直接写回调用页面,如下所示:

public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "text/plain";
    //context.Response.Write("Hello World");
    string name = context.Request.Form["contactname"];
    string email = context.Request.Form["contactemail"];
    string content = context.Request.Form["contactmessage"];
    try
    {
        SendEmail(name, email, content);
    }
    catch (Exception ex)
    {
       // Write exception message to user
       // You can include some of the exception detail via the ex object, 
       // but be careful how much information you divulge to the user 
       context.Response.Write("There was a problem sending the email.");
    }

    // Email was sent successfully
    context.Response.Write("Email sent successfully.");
}

【讨论】:

  • 它只在 .ashx 页面中打印结果。我正在尝试将响应带到同一个 html 页面或另一个 html 页面
【解决方案2】:

我知道这是一个相当古老的问题,但它仍然没有一个公认的答案,更不用说我认为最适合这种情况的答案了。 OP 声明的表单的目的是简单地根据 HTML 表单的输入发送电子邮件,并在适当时提供成功/失败消息。这不是 ASP.NET HTTP 处理程序的合适情况。我知道,HTTP 处理程序不会让您简单地通过 POST 回到 Web 表单,这并没有什么好处。但是,在 Web 表单中使用回发​​处理程序可以让您轻松地执行以下操作:

  1. 成功后重定向
  2. 成功时显示消息
  3. 出现错误时显示带有详细信息的消息
  4. 出错时重定向,不详细说明

例如,您可能有一个通常不可见的 ASP.NET 面板控件,但是当您在验证表单或发送电子邮件时遇到错误时,您可以在那里显示错误消息。如果您尝试使用 HTTP 处理程序来执行此操作,您要么必须做太多繁重的工作,将错误消息注入同一页面或另一个页面,要么您有一个非常平淡的纯文本消息要发送给客户端。

如果您必须使用 HTTP 处理程序,我建议您使用 Karl Anderson 的 answer,并将 至少 context.Response.StatusCode 设置为 HTTP 状态代码这对错误是有意义的。正如答案所示,如果SendEmail() 方法抛出异常,HTTP 处理程序会将错误消息作为 HTTP 200 (OK) 发送回客户端,如果出现异常并且无法发送电子邮件,则这是不正确的.我没有测试过这种方法(使用context.Response.Write()),但我相信你最终会得到非常基本的文本输出到浏览器作为对提交表单产生的POST请求的响应。不会有任何 HTML,这取决于您的应用程序如何配置来处理 HTTP 错误。如果您有 HTTP 500 的自定义错误处理程序,并且您配置了 catch 块以将 StatusCode 设置为 500,那么您必须配置该自定义处理程序页面以读取响应消息,以便将该消息注入您的自定义处理程序页面内容。

【讨论】:

    猜你喜欢
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多