ScriptMethodAttribute 类


指定用于调用方法的 HTTP 谓词以及响应的格式。Specifies which HTTP verb is used to invoke a method, and the format of the response. 此类不能被继承。This class cannot be inherited.

public ref class ScriptMethodAttribute sealed : Attribute
public sealed class ScriptMethodAttribute : Attribute
type ScriptMethodAttribute = class
    inherit Attribute
Public NotInheritable Class ScriptMethodAttribute
Inherits Attribute


下面的示例演示如何将应用ScriptMethodAttribute GetXmlDocument于和EchoStringAndDate方法, 以指定要使用的 HTTP 谓词, 并指定响应格式。The following example shows how to apply ScriptMethodAttribute to the GetXmlDocument and EchoStringAndDate methods to specify which HTTP verb to use and to specify the response format.


此属性用于指定可从客户端脚本中调用的方法的信息。This attribute is used to specify information for methods that can be called from client script. 特性使你可以指定可用于调用方法的 HTTP 谓词 (GET 或 POST)。The attribute lets you specify which HTTP verb (GET or POST) can be used to invoke a method. 它还允许您指定是否使用 JavaScript 对象表示法 (JSON) 或 XML 来设置响应的格式。It also lets you specify whether the response is formatted by using either JavaScript Object Notation (JSON) or XML.

ScriptMethodAttribute 属性是可选项。The ScriptMethodAttribute attribute is optional. (但是, 可以从客户端脚本调用的方法必须System.Web.Services.WebMethodAttribute应用特性。)。(However, methods that can be called from client script must have the System.Web.Services.WebMethodAttribute attribute applied.). 如果未用ScriptMethodAttribute标记方法, 则将使用 HTTP POST 命令调用方法, 并且响应将序列化为 JSON。If a method is not marked with ScriptMethodAttribute, the method will be called by using the HTTP POST command and the response will be serialized as JSON. 不能从脚本中覆盖此设置。You cannot override this setting from script.

ScriptMethodAttribute仅采用命名参数。ScriptMethodAttribute takes only named parameters. 下面是可用作命名参数的ScriptMethodAttribute类的属性:The following are the properties of the ScriptMethodAttribute class that can be used as named parameters:

  • UseHttpGet

    指定是否将使用 HTTP GET 命令调用方法。Specifies whether the method will be invoked by using the HTTP GET command. 默认值为 falseThe default is false.


    如果使用敏感数据true或事务, 则将属性设置为可能会给应用程序带来安全风险。UseHttpGetSetting the UseHttpGet property to true might pose a security risk for your application if you are working with sensitive data or transactions. 在 GET 请求中, 浏览器将消息编码到 URL 中, 因此更易于进行篡改。In GET requests, the message is encoded by the browser into the URL and is therefore an easier target for tampering. 对于 GET 和 POST 请求, 应遵循安全准则来保护敏感数据。For both GET and POST requests, you should follow security guidelines to protect sensitive data.

  • ResponseFormat

    指定是否将响应序列化为 JSON 或 XML。Specifies whether the response will be serialized as JSON or as XML. 默认值为 JsonThe default is Json. 当方法XmlDocument返回或XmlElement对象时,属性在指定XML作为返回类型时非常有用。ResponseFormatThe ResponseFormat property is useful to specify XML as the return type when the method returns an XmlDocument or an XmlElement object.

  • XmlSerializeString

    指定是否所有返回类型 (包括字符串类型) 都序列化为 XML。Specifies whether all return types, including string types, are serialized as XML. 当响应序列化为XmlSerializeString JSON 时, 属性的值将被忽略。The value of the XmlSerializeString property is ignored when the response is serialized as JSON.

有关如何使用特性的详细信息, 请参阅特性For more information about how to use attributes, see Attributes.



初始化 ScriptMethodAttribute 类的新实例。Initializes a new instance of the ScriptMethodAttribute class.



获取或设置方法响应的格式。Gets or sets the format of the method response.


在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

获取或设置一个值,该值指示是否通过使用 HTTP GET 调用方法。Gets or sets a value that indicates whether to invoke the method by using HTTP GET.


获取或设置一个值,该值指示是否将所有返回类型都序列化为包含字符串类型的 XML。Gets or sets a value that indicates whether all return types are serialized as XML, which includes string types.



返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)


_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(继承自 Attribute)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)