MVC 6 中的 Web API 缺少 $id

Web API in MVC 6 is missing $id

我注意到,与 MVC 5 中返回的每个对象都包含一个 $id 属性 的 Web API 相比,在 MVC 6 中没有返回这样的 id。由于我的客户端代码使用了这个 属性,是否有任何设置可以让它在 MVC 6 中返回?

我认为你提的问题不完全正确,但我想如果你修改Startup.csConfigureServices方法,你可以得到和以前一样的结果。尝试将 Newtonsoft.Json 使用的 PreserveReferencesHandling 设置为值 PreserveReferencesHandling.Objects:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .AddJsonOptions(options => {
            options.SerializerSettings.PreserveReferencesHandling =
                PreserveReferencesHandling.Objects;
        });
    ...
}

这样的设置不会在真正的所有对象中添加$id,但我认为它应该适用于您之前使用过的情况。