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

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

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

[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”告诉它应该有一个额外的 token 参数需要验证。我基本上想避免将此代码复制到每个需要它的方法:

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

    // Do something.
}

我有点不知道从哪里开始。谁能指出我如何在不丢失 asmx 处理已有的任何功能的情况下添加此功能的正确方向?

最佳答案

你可以把属性放在方法上,然后有一个Soap Extension检查属性并相应地进行操作。

关于c# - 将自定义属性添加到 ASMX Web 服务方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8992208/

相关文章:

c# - 使用内部应用程序的模型将公共(public)表单提交到安全的内部应用程序

asp.net - "Unable to determine the provider name"mvc-mini-profiler 1.9 错误

c# - 如何使用 VS2010 安装项目将我的程序添加到用户开始菜单?

c# - Linq 查询 - 创建通用子集

c# - .NET 窗体 : prevent button click trigger for a multiline textbox on a "AcceptButton" enabled form?

c# - 如何计算字符串中每个单词的出现次数?

javascript - 是否可以在 asp.net 中使用 javascript 创建服务器端列表框控件?

c# - 具有复杂类型参数的 WebInvoke 不适用于 Json 端点

ios - 在没有互联网时防止 JSONSerialization 崩溃我的应用程序的最佳方法?

c# - 无法通过 C# 访问 C++/CLI 重载运算符