从ASP.Net MVC进行异步WebService调用

时间:2021-12-17 13:22:28

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

我会(如问题所述)喜欢进行异步调用,最好使用ASP.net AJAX。

The code for the WebMethod looks like this:

WebMethod的代码如下所示:

[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 on the view are, not surprisingly: name, email, subject, body.

视图中的字段毫不奇怪:名称,电子邮件,主题,正文。

In my attempts to do this I haven't been able to get to the WebMethod. The service reference is in place, so at least I didn't screw that up.

在我尝试这样做的过程中,我无法访问WebMethod。服务参考已经到位,所以至少我没有搞砸了。

Thanks for the help...

谢谢您的帮助...

3 个解决方案

#1


0  

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

在这里,你可以找到一个用ASP.NET MVC中的AJAX调用异步方法的例子

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

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

<% } %>

You can receive the params in the controller method and call the webservice from there.

您可以在控制器方法中接收参数并从那里调用Web服务。

#2


3  

This is not an answer to your question but a warning. I was looking at this method and thinking, "hmm, I wonder if ASP.NET cares if a call to this web method comes from your site or somewhere else?" A quick google search leads me to believe that there isn't any checking to make sure some douche isn't using your web methods for his own malicious desires (here's a blog post talking about this).

这不是你问题的答案,而是一个警告。我正在研究这种方法并思考,“嗯,我想知道ASP.NET是否关心这个网络方法的调用是来自你的网站还是其他地方?”一个快速的谷歌搜索让我相信,没有任何检查,以确保一些冲洗没有使用您的网络方法为他自己的恶意欲望(这是一篇博客文章谈论这个)。

So, before you get this working, you might want to think about ways to prevent someone from hijacking your webmethod to send me Viagra emails. Because if I get a Viagra email from your website, I'm not going to be very happy with you.

因此,在您开始工作之前,您可能想要想办法防止有人劫持您的网络方法向我发送伟哥电子邮件。因为如果我从您的网站收到伟哥电子邮件,我将不会对您感到满意。

#3


-3  

According to the MSDN Library

根据MSDN Library

In order for a Web service to be accessed from script, it must be an .asmx Web service whose Web service class is qualified with the ScriptServiceAttribute attribute. Individual methods to be called from script must be qualified with the WebMethodAttribute attribute.

为了从脚本访问Web服务,它必须是.asmx Web服务,其Web服务类使用ScriptServiceAttribute属性进行限定。必须使用WebMethodAttribute属性限定从脚本调用的各个方法。

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

#1


0  

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

在这里,你可以找到一个用ASP.NET MVC中的AJAX调用异步方法的例子

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

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

<% } %>

You can receive the params in the controller method and call the webservice from there.

您可以在控制器方法中接收参数并从那里调用Web服务。

#2


3  

This is not an answer to your question but a warning. I was looking at this method and thinking, "hmm, I wonder if ASP.NET cares if a call to this web method comes from your site or somewhere else?" A quick google search leads me to believe that there isn't any checking to make sure some douche isn't using your web methods for his own malicious desires (here's a blog post talking about this).

这不是你问题的答案,而是一个警告。我正在研究这种方法并思考,“嗯,我想知道ASP.NET是否关心这个网络方法的调用是来自你的网站还是其他地方?”一个快速的谷歌搜索让我相信,没有任何检查,以确保一些冲洗没有使用您的网络方法为他自己的恶意欲望(这是一篇博客文章谈论这个)。

So, before you get this working, you might want to think about ways to prevent someone from hijacking your webmethod to send me Viagra emails. Because if I get a Viagra email from your website, I'm not going to be very happy with you.

因此,在您开始工作之前,您可能想要想办法防止有人劫持您的网络方法向我发送伟哥电子邮件。因为如果我从您的网站收到伟哥电子邮件,我将不会对您感到满意。

#3


-3  

According to the MSDN Library

根据MSDN Library

In order for a Web service to be accessed from script, it must be an .asmx Web service whose Web service class is qualified with the ScriptServiceAttribute attribute. Individual methods to be called from script must be qualified with the WebMethodAttribute attribute.

为了从脚本访问Web服务,它必须是.asmx Web服务,其Web服务类使用ScriptServiceAttribute属性进行限定。必须使用WebMethodAttribute属性限定从脚本调用的各个方法。

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