Making an asynchronous WebService call from ASP.Net MVC

I would like (as the question says) to make an asynchronous call, preferably using ASP.net AJAX.

The code for the WebMethod looks like this:

[WebMethod]
public void SendMail(string name, string email, string subject, string body)
{
  MailMessage toSend = new MailMessage(email, address@domain.com, subject, body);
  var smtp = new SmtpClient();
  smtp.Send(toSend);
}

      

The fields in the view are not surprising: name, email, subject, body.

In my attempts to do this, I was unable to get to the WebMethod. The service link is in place, so at least I didn't blame it.

Thanks for the help...

0


source to share


3 answers


Here you can find an example of calling asynchronous methods with AJAX in ASP.NET MVC with elements like

<% using (Ajax.Form("SendMail", new AjaxOptions { UpdateTargetId = "resultDiv" })) { %>

   <!-- Your form elements here... -->

<% } %>

      



You can get parameters in the controller method and call the web service from there.

0


source


This is not an answer to your question, but a warning. I looked at this method and thought, "Hmm, I'm wondering if ASP.NET cares if calling this web method from your site or somewhere else?" A quick google search leads me to believe that there is no validation to make sure some soul is not using your web methods for his own evil desires ( here's a blog post talking about this ).



So, before you get started, you might need to think about how to prevent someone from hijacking your web method from sending me Viagra emails. Because if I receive Viagra email from your site, I won't be very happy with you.

+3


source


According to MSDN library

For a web service to be accessed from a script, it must be an .asmx Web service whose web service class is qualified with the ScriptServiceAttribute. The individual methods to be called from the script must be qualified with the WebMethodAttribute.

see http://msdn.microsoft.com/en-ca/library/bb398998.aspx

-3


source







All Articles