将自定义属性添加到ASMX Web服务方法

时间:2012-01-24 18:40:35

标签: c# asp.net web-services attributes httphandler

我在asmx Web服务中有一些Web方法,目前看起来像这样:

[WebMethod(false, System.EnterpriseServices.TransactionOption.NotSupported)]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public XElement GetSomeData(int dataId)
{
    // Do something.
}

我希望能够做到这样的事情:

[WebMethod(false, System.EnterpriseServices.TransactionOption.NotSupported)]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
[EnableSomeCustomSecurityCheck(true)]
public XElement GetSomeData(int dataId)
{
    // Do something.
}

其中“EnableSomeCustomSecurityCheck”告诉它应该有一个需要验证的附加令牌参数。我基本上想避免将此代码复制到需要它的每个方法:

[WebMethod(false, System.EnterpriseServices.TransactionOption.NotSupported)]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public XElement GetSomeData(int dataId, string securityToken)
{
    SomeClass.CheckSecurityToken(securityToken);

    // Do something.
}

我有点迷失在哪里开始。任何人都可以指出我正确的方向,我可以添加此功能,而不会丢失asmx处理已有的任何功能?

1 个答案:

答案 0 :(得分:1)

您可以将该属性放在方法上,然后使用Soap Extension检查属性并相应地执行操作。