在C#中有条件地隐藏asmx Web方法

在C#中有条件地隐藏asmx Web方法

问题描述:

如何有条件地隐藏Web方法?如果web.config中的标志设置为false,我不想公开Web方法.如果是真的,那么我将公开该web方法.

How can I conditionally hide the web methods? I don't want to expose the web method if a flag setup in web.config is false. If it is true, then I will expose the web method.

含义:该方法仅在标志为true时对客户端可见,否则对客户端不可用.

Meaning: The method should only be visible to the client if the flag is true otherwise it should not be available for the client.

这在C#中可能吗?

您不能真正做到这一点.

You can't really do this.

您的客户端看到" Web方法,因为它们在WSDL中列出. WSDL的生成是相当静态的-它基于方法上的[WebMethod]属性.确实无法从生成的WSDL中删除项目.

Your clients "see" the web methods because they are listed in the WSDL. The WSDL generation is fairly static - it is based on the [WebMethod] attributes on your methods. There is really no ability to remove items from the generated WSDL.

如果您可以切换到WCF,您会发现这非常简单.同时,您只需要使用授权-阻止某些客户端调用某些方法.

If you can switch to WCF, you will find this much, much, easier. In the meantime, you'll just have to use authorization - prevent some clients from calling some methods.