Asp.net web api Get(id)与GetAll()的不同类型

Asp.net web api Get(id)与GetAll()的不同类型,asp.net-web-api,Asp.net Web Api,我正在创建一个非常简单的web api,允许我搜索: public IEnumerable<Thing> GetAllThings() { // get all the things! } 检索单个对象时,我需要比检索所有对象时更多的详细信息。我是否应该有一个单独的控制器来返回带有细节的东西,而不是在GetAllThings和GetThing上有单独的模型 不需要。您不必为详细信息制作单独的控制器。只是有一个不同的重载方法。将根据请求调用以下所有方法 当您使用url:api

我正在创建一个非常简单的web api,允许我搜索:

public IEnumerable<Thing> GetAllThings()
{
    // get all the things!
}

检索单个对象时,我需要比检索所有对象时更多的详细信息。我是否应该有一个单独的控制器来返回带有细节的东西,而不是在GetAllThings和GetThing上有单独的模型

不需要。您不必为详细信息制作单独的控制器。只是有一个不同的重载方法。将根据请求调用以下所有方法

当您使用url:api/{controller}时,将调用以下方法

public IEnumerable<Thing> GetAllThings()
{
    // get all the things!
}
当您使用url:api/{controller}?id=1&name=xyz时,将调用下面的方法

public Thing GetThing(string id, string name)
{
    // get a single thing
}

作为最佳实践,您应该有一个包含所有可能属性的
Thing
实体,这样您就可以保持代码的一致性,并且默认情况下您可以加载所有属性(假设它们来自DB)

如果您想“隐藏”某些属性,可以在返回它们之前使用扩展方法将它们设置为null

public class Thing
{
    public string PropertyA { get; set; }
    public string PropertyB { get; set; }
    public string PropertyC { get; set; }
}

// extension methods
public static class ThingExtensionMethods
{
    public static void ToBasicDetails(this Thing thing)
    {
        // hide properties
        thing.PropertyB = null;
        thing.PropertyC = null;
    }
}

public IEnumerable<Thing> GetAllThings()
{
    var things = _db.Things.ToList();
    foreach(var item in things)
    {
        item.ToBasicDetails();
    }

    return things;
}

public Thing GetThing(string id)
{
    var thing = _db.Things.Find(id);
    return thing;
}
公共类的东西
{
公共字符串属性{get;set;}
公共字符串属性b{get;set;}
公共字符串PropertyC{get;set;}
}
//扩展方法
公共静态类ThingExtensionMethods
{
公共静态无效详细信息(这件事)
{
//隐藏属性
thing.PropertyB=null;
thing.PropertyC=null;
}
}
公共IEnumerable GetAllThings()
{
var things=_db.things.ToList();
foreach(事物中的var项)
{
项目。详细信息();
}
归还物品;
}
获取公共对象(字符串id)
{
var thing=_db.Things.Find(id);
归还物;
}
定义单独的“Thing”和“ThingWithDetails”模型是有意义的,因为您将向客户端返回不同的资源类型。返回类型定义了响应主体,因此对不同的响应格式使用不同的模型

您不需要将这些方法放在单独的控制器中——一个操作返回ThingWithDetails,另一个操作返回IEnumerable是有效的。这归结为偏好。(我可能会把它们放在同一个控制器中,因为它们处理的是同一个DB实体。)


本教程给出了一个示例。

那么,您是否建议在返回一个事物列表时包含我不希望包含的属性,并将其保留为默认值?理想情况下,编码实践表明,特定控制器的模型应该具有该API所需的所有可能属性。如果你想返回完全不同的模型,那么最好为它创建一个不同的控制器。问题是我不想加载的细节会给数据库带来太大的压力,无法加载到列表中。它们来自不同的表。然后,您可以以相同的方式使用扩展方法,并在需要时加载额外的详细信息,或者在需要时直接从控制器加载这些详细信息。这种方法的问题是,客户端在响应中会得到一堆空字段。您确实需要DTO。@MikeWasson您可以将json格式化程序(json.NET)配置为忽略空属性
public Thing GetThing(string id, string name)
{
    // get a single thing
}
public class Thing
{
    public string PropertyA { get; set; }
    public string PropertyB { get; set; }
    public string PropertyC { get; set; }
}

// extension methods
public static class ThingExtensionMethods
{
    public static void ToBasicDetails(this Thing thing)
    {
        // hide properties
        thing.PropertyB = null;
        thing.PropertyC = null;
    }
}

public IEnumerable<Thing> GetAllThings()
{
    var things = _db.Things.ToList();
    foreach(var item in things)
    {
        item.ToBasicDetails();
    }

    return things;
}

public Thing GetThing(string id)
{
    var thing = _db.Things.Find(id);
    return thing;
}