问题背景:
我有一个简单的联系表格'在我的页面上。用户需要提供他们的姓名,通过电子邮件发送消息主题以及实际消息本身。
我目前正在尝试实施“错误”#39;在邮件过程中的任何错误期间都会显示视图,例如您提供的电子邮件格式不正确。
问题:
我在家庭控制器上使用MVC HandleError
属性,目前在发生错误时未显示该页面。
代码: 这是Contact表单,它使用AJAX将请求提交到Home Controller上的Action方法:
@using (Ajax.BeginForm("SendFMFCMail", "Home", new AjaxOptions { UpdateTargetId = "result", OnBegin = "LoadingSpinner", OnSuccess = "MessageSent", OnFailure = "FailedMail" }))
{
@Html.ValidationSummary(true)
<div id="result"></div>
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<div class="row">
<div class="col-md-6 form-group">
@Html.TextBoxFor(m => m.Name, new { @class = " form-control spanWidth", placeHolder = "Name", id = "name" })
@Html.ValidationMessageFor(model => model.Name)
@*<input class="form-control spanWidth" id="name" placeholder="Name">*@
</div>
<div class="col-md-6 form-group">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control spanWidth", placeHolder = "Email", id = "email" })
@Html.ValidationMessageFor(model => model.Email)
</div>
</div>
<div class="row">
<div class="col-sm-12 form-group">
@Html.TextBoxFor(m => m.Subject, new { @class = "form-control spanWidth", placeHolder = "Subject", id = "subject" })
@Html.ValidationMessageFor(model => model.Subject)
</div>
</div>
<div class="row">
<div class="col-sm-12 form-group">
@Html.TextAreaFor(m => m.Message, new { @class = "form-control spanHeight spanWidth", placeHolder = "Message", rows = 15, id = "message" })
@Html.ValidationMessageFor(model => model.Message)
</div>
</div>
<div class="row">
<div class="col-sm-12 form-group">
<input type="submit" class="btn btn-primary btn-block spanWidth" value="Send Message" />
</div>
</div>
</div>
</div>
</div>
Error.cshtml
视图位于项目Shared
文件夹中。
我已确保Web.config
CustomErrors
已转为On
:
<customErrors mode="On" />
这是FilterConfig.cs
:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
在HomeController
中,我尝试在类的顶部设置一般异常处理程序:
[HandleError(View = "FMFCError")]
public class HomeController : Controller
{
//code.
}
我也尝试在我的Action
方法上设置它,这也不起作用:
注意:我设置了一个明确的异常类型SmtpException
,因为这是我发送电子邮件时发送的电子邮件地址无效的故障;
[HandleError(ExceptionType = typeof(SmtpException), View = "FMFCError")]
public ActionResult SendFMFCMail(Contact contactDetails)
{
var emailHandler = new EmailHandler();
try
{
emailHandler.SendMail(contactDetails);
}
catch(SmtpException ex)
{
throw ex;
}
return Json(new { messageStatus = "success" });
}
这是SendMail
方法:
public void SendMail(Contact emailMessage)
{
string sellerEmail = WebConfigurationManager.AppSettings["SellerEmail"];
MailAddress from = new MailAddress(emailMessage.Email, emailMessage.Name);
MailAddress to = new MailAddress(sellerEmail);
MailMessage mail = new MailMessage(from, to);
mail.Subject = emailMessage.Subject;
mail.Body = emailMessage.Message;
SmtpClient client = new SmtpClient()
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(from.Address, WebConfigurationManager.AppSettings["SellerEmailPassword"])
};
client.Send(mail);
}
谁能看到我出错的地方?
答案 0 :(得分:1)
您无法通过AJAX帖子重定向。您可以将URL发送回要将浏览器重定向到的前端,然后使用JS在那里导航。
后端 - 家庭控制器
[HttpPost]
public ActionResult GoToMethodName()
{
return Json(Url.Action("Index", "MethodName"));
}
Views JS - 这是一个简单的代码,假设您将重定向异常或成功,可以根据您希望如何处理成功来编写更复杂的逻辑处理,这将修复您对异常捕获的需求的重定向。
$.ajax({
type: "POST",
url: "http://localhost/GoToMethodName",
dataType: 'json',
crossDomain: true,
success: function(data){
window.location.href = data;
}
});