WebMethod 与 ScriptMethod [英] WebMethod vs ScriptMethod

查看:65
本文介绍了WebMethod 与 ScriptMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 [WebMethod] 属性标记的方法的 .NET 3.5 aspx 位置.我用 jQuery 调用它,双向发送 JSON.这一切都很好.我的问题是,将 [ScriptMethod] 应用于方法时会做什么?我已经试过了,它似乎产生了相同的结果.ScriptMethodWebMethod 是否相同且可以互换,或者其中一个提供另一个不提供的功能和/或开销?总的来说,我发现自己对所有可用于实现网络服务的选项感到困惑,我想知道每个选项的优缺点是什么.

I have a .NET 3.5 aspx place with a method marked with the [WebMethod] attribute. I'm calling this with jQuery, sending JSON in both directions. This all works great. My question is, what does [ScriptMethod] do when applied to an method? I've tried this and it seems to yield the same result. Are ScriptMethod and WebMethod identical and interchangeable, or does one provide functionality and/or overhead that the other doesn't? In general, I find myself confused with all of the options available for implementing web services and I'd like to know what the pros and cons are for each.

推荐答案

您在以下 2 个场景中使用 ScriptMethod 属性.

You use the ScriptMethod attribute in the following 2 scenarios.

  1. 您正在使用 jquery 或任何其他 ajax 请求机制,但您希望请求是 GET 而不是 POST.
  2. 您想在 javaScript 中接收 XML 格式的响应.

如果您没有上述要求之一;您只需要使用 ajax 请求的 JSON 响应,然后您就可以简单地使用 WebMethod.

If you don't have one of the above requirements; you just need a JSON response using a an ajax request then you can simply use the WebMethod.

这里还有一个更让人困惑的元素,什么时候使用ScriptService属性?如果您使用的是 Microsoft Ajax 客户端脚本框架,则使用此属性,此属性告诉服务器在客户端生成代理对象,以便您可以像调用普通对象一样调用函数.var MyRemoteObject = new RemoteObject(); MyRemoteObject.getMessage(....) 甚至当您使用 ScriptService 属性时,您也不需要仅添加 ScriptMethod 属性在上述场景中.

There is still one more confusing element here, when do you use the ScriptService attribute? this is used if you are using the Microsoft Ajax Client script framework, this attributes tell the server to generate proxy objects on the client so that you can call functions just like a normal object. var MyRemoteObject = new RemoteObject(); MyRemoteObject.getMessage(....) and even when you use the ScriptService attribute you don't need to add the ScriptMethod attribute only in the above scenarios.

一开始我很困惑,因为我认为 ScriptService 和 ScriptMethod 属性可以一起工作,就像 WebService 和 WebMethod 属性一样.

It was confusing to me at the begining because i thought that the ScriptService and the ScriptMethod attributes works together just like the WebService and WebMethod attributes.

这篇关于WebMethod 与 ScriptMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆