c# - MVC HandlerError attribute isn't redirecting to error View on Exception -
question background:
i have simple 'contact form' on page. user needs supply name, email message subject , actual message itself.
i'm trying implement 'error' view display during errors on mailing process such incorrect email format uer supplys.
the issue:
i'm using mvc handleerror
attribute on home controller, page not being shown when error thrown.
code: contact form uses ajax submit request action method on home controller:
@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>
the error.cshtml
view in projects shared
folder.
i have made sure web.config
has customerrors
turned on
:
<customerrors mode="on" />
this filterconfig.cs
:
public static void registerglobalfilters(globalfiltercollection filters) { filters.add(new handleerrorattribute()); }
in homecontroller
have tried setting general exception handler @ top of class:
[handleerror(view = "fmfcerror")] public class homecontroller : controller { //code. }
and have tried setting on action
method, not work:
note: have set explicit exception type of smtpexception
i'm expecting fail when email sent invalid email address;
[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" }); }
this sendmail
method:
public void sendmail(contact emailmessage) { string selleremail = webconfigurationmanager.appsettings["selleremail"]; mailaddress = new mailaddress(emailmessage.email, emailmessage.name); mailaddress = 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); }
can see i'm going wrong?
you cannot redirect via ajax post. can send url frontend want redirect browser , use js navigate there.
backend - home controller
[httppost] public actionresult gotomethodname() { return json(url.action("index", "methodname")); }
views js - simple code assumes redirect on exception or success, more complex logic handling can written depending on how handle success, fix redirect on exception caught needs.
$.ajax({ type: "post", url: "http://localhost/gotomethodname", datatype: 'json', crossdomain: true, success: function(data){ window.location.href = data; } });
Comments
Post a Comment