问题描述
我有一个使用 OData v4 (System.Web.OData) 用 C# 构建的 OData 服务.该服务有一些安全限制:只有经过身份验证才能访问的集合.出于包括测试自动化在内的多种原因,我希望该服务能够在其 $metadata 中声明这些限制.
I have an OData service constructed in C# using OData v4 (System.Web.OData). There are some security restrictions on the service: collections which you're only allowed to access if you're authenticated. For a number of reasons including test automation I'd like for the service to be able to declare those restrictions in its $metadata.
这可以通过 OData 注释实现吗?
Is this possible with OData annotations?
推荐答案
答案:使用 DirectValueAnnotationsManager 将注释设置为 Edm*Constant.
Answer: use the DirectValueAnnotationsManager to set the annotation to an Edm*Constant.
EdmModel model = myEntityModel;
EdmEntitySet = myEntitySet;
model.DirectValueAnnotationsManager.SetAnnotationValue(entitySet, "MyNamespace", "AdminOnly", new EdmBooleanConstant(EdmCoreModel.Instance.GetBoolean(false), true));
请注意,EdmBooleanConstant 和 EdmStringConstant 都在 $metadata 上生成完全相同的 XML 输出.
Note that EdmBooleanConstant and EdmStringConstant both produce exactly the same XML output on $metadata.
这篇关于如何声明自定义 oData 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!