【问题标题】:Web Api json response $id variableWeb Api json 响应 $id 变量
【发布时间】:2014-06-02 19:48:57
【问题描述】:

在 asp web api 中,我有这个控制器来响应 ajax 请求并发送回 json 数据:

public IEnumerable<PersonaleDTO> GetPersonale()
{
    var n = (from p in db.Personale
             select new PersonaleDTO { Id = p.Id, Nome = p.Nome, Cognome = p.Cognome Cellulare = p.Cellulare, Email = p.Email, Attivo = (bool)p.Attivo }).ToList();
    return n;
}

这个方法返回7个对象,是正确的。

现在,当我收到 json 格式的数据时,我看到我还收到了 $id 成员以及 id、Nome、Cognome...

$id 变量是什么?如何从 json 响应中删除它?

【问题讨论】:

    标签: c# asp.net-mvc json angularjs asp.net-web-api


    【解决方案1】:

    WebApiConfig试试这个代码

    var json = config.Formatters.JsonFormatter;
    json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
    

    编辑:如果由于某种原因您正在使用自定义 ContractResolver,那么按照 post

    自定义 ContractResolver 设置会覆盖 PreserveReferencesHandling 设置。

    在你的 DefaultContractResolver/IContractResolver 实现中,添加这个;

    public override JsonContract ResolveContract(Type type) {
        var contract = base.ResolveContract(type);
        contract.IsReference = false;
        return contract;
    }
    

    这与没有自定义 ContractResolver 的 PreserveReferencesHandling.None 设置类似。

    【讨论】:

    • 他还问“$id 变量是什么”...你有答案吗?
    猜你喜欢
    • 1970-01-01
    • 2021-02-09
    • 2020-05-03
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多