C# 如何允许客户机指定他们希望从我的api返回的字段?

C# 如何允许客户机指定他们希望从我的api返回的字段?,c#,.net,asp.net-web-api2,C#,.net,Asp.net Web Api2,我想模仿一些流行的api,我经常看到的一件事是客户端查询web api并指定要返回的字段。例如,查询用户的详细信息可能有大量信息,或者只是基本信息。我不想为所有不同级别的细节创建单独的端点,而是希望能够允许客户机请求他们所需的信息。这是一个私有api 因此,在服务器上,我只想使用一个用户完整的数据(急切加载或延迟加载;它还没有确定),但一旦我有了这个对象,我想使用客户机请求中指定的字段来构建响应对象 这里有图书馆吗?或者任何流行的技术或文章都会有所帮助。TIA这是一种复杂的过程,但基本上,如果传

我想模仿一些流行的api,我经常看到的一件事是客户端查询web api并指定要返回的字段。例如,查询用户的详细信息可能有大量信息,或者只是基本信息。我不想为所有不同级别的细节创建单独的端点,而是希望能够允许客户机请求他们所需的信息。这是一个私有api

因此,在服务器上,我只想使用一个用户完整的数据(急切加载或延迟加载;它还没有确定),但一旦我有了这个对象,我想使用客户机请求中指定的字段来构建响应对象


这里有图书馆吗?或者任何流行的技术或文章都会有所帮助。TIA

这是一种复杂的过程,但基本上,如果传入属性,您只希望循环返回的对象,并返回这些属性的字典。我为此创建了一个扩展方法

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace YourNamespace.Extensions
{
    public enum PropertyFormat
    {
        AsIs,
        PascalCase,
        CamelCase
    }

    public static class DataShapingExtensions
    {
        public static object ToDataShape<ObjectIn>(this ObjectIn objectToShape, string fields, PropertyFormat propertyFormat = PropertyFormat.AsIs) where ObjectIn : class
        {
            var listOfFields = new List<string>();

            if (!string.IsNullOrWhiteSpace(fields))
            {
                listOfFields = fields.ToLower().Split(',').ToList();
            }

            if (listOfFields.Any())
            {
                var objectToReturn = new JObject();

                //====
                var enumerable = objectToShape as IEnumerable;

                if (enumerable != null)
                {
                    var listOfObjects = new List<JObject>();

                    foreach (var item in enumerable)
                    {
                        var objectToReturn2 = new JObject();

                        listOfFields.ForEach(field =>
                        {
                            try
                            {
                                var prop = item.GetType()
                                    .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

                                var fieldName = prop.Name;
                                var fieldValue = prop.GetValue(item, null);

                                fieldName = GetName(fieldName, propertyFormat);
                                objectToReturn2.Add(new JProperty(fieldName, fieldValue));
                            }
                            catch (Exception ex) { }
                        });

                        listOfObjects.Add(objectToReturn2);
                    }

                    return listOfObjects.ConvertAll(o => o);
                }
                //====

                listOfFields.ForEach(field =>
                {
                    try
                    {
                        var prop = objectToShape.GetType()
                            .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

                        var fieldName = prop.Name;
                        var fieldValue = prop.GetValue(objectToShape, null);

                        fieldName = GetName(fieldName, propertyFormat);
                        objectToReturn.Add(new JProperty(fieldName, fieldValue));
                    }
                    catch (Exception ex) { }
                });

                return objectToReturn;
            }

            return objectToShape;
        }

        private static string GetName(string field, PropertyFormat propertyFormat)
        {
            switch (propertyFormat)
            {
                case PropertyFormat.AsIs: return field;
                case PropertyFormat.PascalCase: return field.ToPascalCase();
                case PropertyFormat.CamelCase: return field.ToCamelCase();
                default: return field;
            }
        }
    }
}
这将返回一个只包含这些字段的对象

{
  field1: "",
  field2: ""
}
$.ajax({
   url: "/api/yourRoute?fields=field1,field2"
})
{
  field1: "",
  field2: ""
}