12

I have this simple ajax form in my MVC3 app, that sends email message:

@using (Ajax.BeginForm("SendMessage", new AjaxOptions { 
    LoadingElementId = "wait", 
    OnSuccess = "loadMessages", 
    OnFailure = "showError" }))
{
    <input type="text" name="message" placeholder="Message" />
    <input type="submit" name="submit" value="Send" />
}

If the action fails to send a message, it throws the exception. The exception is handled and displayed in the showError(error){} javascript function:

function showError(error) { 
    $("#error").text(error.responseText);
};

The problem is: error.responseText is the html dump of the entire error page.

I need to display only the exception error message (whatever is in ex.Message in MVC3).

The implementation of "public ActionResult SendMessage(string message) { }" is irrelevant.

I need to know how to display the exception's message only in showError(error) javascript handler.

Thanks a bunch.

    [HttpPost]
    public ActionResult SendMessage(string message)
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress("sender@gmail.com");
        mail.Subject = "Some Message";
        mail.Body = message;
        mail.To.Add("recepient@gmail.com");
        SmtpServer.Send(mail);
        return null;
    }
4

3 回答 3

6

您无法处理 OnFailure 方法中的每一个异常。因为如果响应状态不在 200 范围内,就会调用OnFailure 。所以你可以像这样处理你的场景:

    [HttpPost]
    public ActionResult SendMessage(string message)
    {
        MailMessage mail = new MailMessage();
        ActionResult response = null;
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress("sender@gmail.com");
        mail.Subject = "Some Message";
        mail.Body = message;
        mail.To.Add("recepient@gmail.com");
        try
        {
            SmtpServer.Send(mail);
            response = Content("Success");
        }
        catch (Exception ex)
        {
            response = Content(ex.Message);
        }
        return response;

    }

在此代码中,如果邮件发送成功,则我已返回响应“成功”,否则我已返回实际错误,并且在 javascript 中我已像这样处理此响应:

    function loadMessages(error) {
    if (error == 'Success') {
        alert("Mail Sent successfully");
    }
    else {
        $("#error").text(error);
    }

希望这会帮助你。

于 2013-05-02T13:08:47.873 回答
2

为了在我自己的解决方案中解决这个问题,我使用了 HttpStatusCodeResult 类。这是我有参考的代码:

try
{
    // Some code that can error
}
catch (Exception e)
{
    return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, e.Message);
}

// View
OnFailure = "alert(error);"

这仍然会触发客户端上的所有正常错误行为,包括在浏览器控制台中放置 500 错误,并且应该与您已经用来显示错误的脚本完美配合。

于 2016-11-15T23:01:04.350 回答
0

您可以在控制器中捕获您的异常并返回确切的错误消息。就像是:

[HttpPost]
public ActionResult SendMessage(string message)
{
    try 
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress("sender@gmail.com");
        mail.Subject = "Some Message";
        mail.Body = message;
        mail.To.Add("recepient@gmail.com");
        SmtpServer.Send(mail);
        return null;
    } 
    catch (e)
    {
        throw new HttpException(500, e.Message);
    }
}

在这种情况下,您可能需要检查响应并showError相应地更新您的 javascript。

于 2013-04-18T17:19:08.187 回答