我们如何在 WebAPI 中隐藏属性? [英] How can we hide a property in WebAPI?

查看:38
本文介绍了我们如何在 WebAPI 中隐藏属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有个模特说

public class Device
{        
        public int DeviceId { get; set; }
        public string DeviceTokenIds { get; set; }
        public byte[] Data { get; set; }
        public string FilePwd { get; set; }        
}

现在我有一个 ASP.net Web API,其中有一个 POST 方法,如下所示

Now I have a ASP.net Web API where there is a POST method as under

[HttpPost]
[Route("AddDeviceRegistrations")]
public void InsertDeviceRegistrations(Device device)

如果我公开 WebAPI,显然所有字段都将可用,例如

If I expose the WebAPI, obviously all the fields will be available e.g.

{
  "DeviceId": 1,
  "DeviceTokenIds": "sample string 2",
  "Data": "QEBA",
  "FilePwd": "sample string 3"
}

我想要的是,每当我公开 WebAPI 时,都不应该公开 DeviceID.我是说我在找

What I want is that, whenever I expose my WebAPI, the DeviceID should not get expose. I mean I am looking for

{

      "DeviceTokenIds": "sample string 2",
      "Data": "QEBA",
      "FilePwd": "sample string 3"
}

有可能吗?如果是这样怎么办?

Is it possible? If so how?

我可以通过将函数签名更改为

I can solve the problem by changing the function signature as

public void InsertDeviceRegistrations(string deviceTokenIds, byte[] data, string FilePwd).

但我想知道这是否可能?如果是这样,如何?

But I wanted to know if it can be possible or not ? If so , how?

提前致谢.

推荐答案

我刚刚发现

[IgnoreDataMember]
 public int DeviceId { get; set; }

命名空间是System.Runtime.Serialization

更多信息 IgnoreDataMemberAttribute班级

今天学到了一些新东西.

Learnt something new today.

谢谢大家.

这篇关于我们如何在 WebAPI 中隐藏属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆