Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何有条件地隐藏 Web 方法?如果 web.config 中的标志设置为 false,我不想公开 web 方法。如果是真的,那我就暴露web方法。
含义:该方法只应在标志为真时对客户端可见,否则它不应该对客户端可用。
这在 C# 中可能吗?
你真的不能这样做。
您的客户“看到”了 Web 方法,因为它们在 WSDL 中列出。WSDL 生成是相当静态的——它基于您的方法的 [WebMethod] 属性。实际上没有能力从生成的 WSDL 中删除项目。
如果您可以切换到 WCF,您会发现这要容易得多。同时,您只需要使用授权 - 阻止某些客户端调用某些方法。
您可以检查方法中的标志,如果未设置则抛出异常。