如何隐藏在网页API 2,ODATA元 [英] how to hide metadata in web api 2, odata
问题描述
我在使用定义 MapODataServiceRoute
ODATA路线我的 WebApiConfig
。
I have defined odata route using MapODataServiceRoute
in my WebApiConfig
.
config.Routes.MapODataServiceRoute("CompanyoOdata", "odata", GetImplicitEdm(config));
private static IEdmModel GetImplicitEdm(HttpConfiguration config)
{
ODataModelBuilder builder = new ODataConventionModelBuilder(config, true);
builder.EntitySet<Company>("Company");
builder.EntitySet<Photo>("Photos");
builder.EntitySet<Country>("Country");
return builder.GetEdmModel();
}
数据服务工作得很好。但我要实现一些事情。
The data service works just fine. But I want to achieve few things.
我不希望因为我使用它在内部暴露我的元数据或协会,将不再需要元数据。我怎么能限制访问这些信息(如限制访问 http://www.sample.com/odata /#元或 http://www.sample.com/odata/ $元)
I don't want to expose my metadata or associations because i'm using it internally and will not need metadata. How can I restrict access to these information (i.e restrict access to http://www.sample.com/odata/#metadata or http://www.sample.com/odata/$metadata)
其次,我想忽略得到序列化的一些属性。我发现这样做有两种方式。
secondly, I want to ignore some properties from getting serialized. I found two ways of doing this.
- 使用数据契约和标记具有属性
[数据成员]
属性或[IgnoreDataMember]
属性 - 使用
忽略
的方法的EntitySet
构建模型时
- Using data contracts and marking properties with
[DataMember]
attribute or[IgnoreDataMember]
attribute - Using
Ignore
method onEntitySet
when building the model
因为我使用的数据库第一种方式为实体框架,因此不能与装饰属性的实体,我不能用第一种方法。我想我可以做到这一点通过使用 MetaDataType
,但似乎它仅适用于 DataAnnotations
。
I can't use the first method as I'm using Database first approach for entity framework hence can't decorate the entity with attributes. I thought I can achieve this by using MetaDataType
, but it seems it only works for DataAnnotations
.
我成功地使用第二种方法,但你不能传递忽略方法不止一个属性。必须这样做是为了个人财产,我需要忽略,这是一个有点乏味。是否有另一种方式来做到这一点?
I used second method with success, but you can't pass more than one property in the ignore method. Has to do it to individual property that I need to ignore, which is a bit tedious. Is there another way to do this?
任何帮助真的AP preciated。
any help really appreciated.
推荐答案
如果要隐藏的元数据(/ $元数据)或服务文件(/),可从现有的路由公约取出的MetadataRoutingConvention,例如:
If want to hide metadata (/$metadata) or service document (/), can remove the the MetadataRoutingConvention from existing routing conventions, e.g.:
var defaultConventions = ODataRoutingConventions.CreateDefault();
var conventions = defaultConventions.Except(
defaultConventions.OfType<MetadataRoutingConvention>());
var route = config.MapODataServiceRoute(
"odata",
"odata",
model,
pathHandler: new DefaultODataPathHandler(),
routingConventions: conventions);
如果只公开每个类型的几个属性,可以使用<一个href=\"http://blogs.msdn.com/b/alexj/archive/2012/08/15/odata-support-in-asp-net-web-api.aspx\">ODataModelBuilder而不是ODataConventionModelBuilder。例如,有些<一个href=\"https://aspnetwebstack.$c$cplex.com/SourceControl/latest#OData/test/System.Web.OData.Test/OData/Builder/ODataModelBuilderTest.cs\">example:
If only expose a few properties per type, can use ODataModelBuilder instead of ODataConventionModelBuilder. E.g., some example:
ODataModelBuilder builder = new ODataModelBuilder();
EntityTypeConfiguration<Customer> customer = builder.EntitySet<Customer>("Customers").EntityType;
customer.HasKey(c => c.Id);
customer.Property(c => c.Name);
这篇关于如何隐藏在网页API 2,ODATA元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!