简体   繁体   English

根据查询字符串返回对象

[英]Return Object Based on Query Strings

I'm trying to return objects based on query strings. 我正在尝试根据查询字符串返回对象。
For example, I would like api/users/{id}?fields=username,email,reputation to return an object of type User that contains only the three included properties (username, email, reputation). 例如,我希望api/users/{id}?fields=username,email,reputation返回类型为User的对象,该对象仅包含三个包括的属性(用户名,电子邮件,信誉)。

On a side note, comma separated query strings are not possible by default in .NET Core. 附带说明一下,.NET Core默认情况下不可能用逗号分隔查询字符串。
Here's a tutorial on making that work . 这是有关使该工作正常进行的教程

Following the guide above, I have a list of strings. 按照上面的指南,我有一个字符串列表。 How can I create an object that only includes the properties that match the names of those strings? 如何创建仅包含与这些字符串的名称匹配的属性的对象?
For only a few strings I can do this (thanks Ben Hall): 我只需要几个字符串就可以做到这一点(感谢Ben Hall):

List<string> listOfStrings ...; // Strings from query
User user = GetUser(id); // User from db 

User newUser = new User();

if (listOfStrings.Contains("username"))
    newUser.username = user.username;
if (listOfStrings.Contains("email"))
    newUser.email = user.email;
if (listOfStrings.Contains("reputation"))
    newUser.reputation = user.reputation;

But for a long list of strings (my user class has 30+ properties) how would I go about doing this? 但是对于一长串字符串(我的用户类具有30多个属性),我将如何去做呢?

For reference, Facebook Graph API does this. 作为参考, Facebook Graph API可以做到这一点。

List<string> listOfStrings ...; // Strings from query
User user = GetUser(id); // User from db 

User newUser = new User();
//gettting object type
var userType = user.GetType();

This solution needs some knowledge about Reflections 此解决方案需要有关反射的一些知识
It uses the methods GetValue and SetValue 它使用方法GetValue和SetValue
(The code is tested in Visual Studio) (代码已在Visual Studio中测试)
Now foreach element in listOfStrings 现在,listOfStrings中的foreach元素

foreach(var propertyName in listOfString){
// This line of code retrives the value of the propety in the user class
 var retrivedValue = userType.GetProperty(propertyName).GetValue(user);
// This line of code sets the value retrived to the property in the newUser class
 userType.GetProperty(propertyName).SetValue(newUser, retrivedValue , null);
}

We could explore other ways like reflection but if should still serve you well enough. 我们可以探索其他方式,例如反思,但是if仍然可以为您提供足够的服务。 Looks like you're making a simple mistake trying to use an If in an object initialiser. 看起来您在尝试在对象初始化程序中使用If犯了一个简单的错误。 You will have to do that after instaniating the new User object eg 在实例化新的User对象之后,您将必须执行此操作,例如

User newUser = new User();
if (listOfStrings.Contains("username"))
{
       newUser.username = user.username;
}

Turns out there is actually Middleware for this called Popcorn. 事实证明,实际上有一个名为Popcorn的中间件。

Popcorn is a communication protocol on top of a RESTful API that allows requesting clients to identify individual fields of resources to include when retrieving the resource or resource collection. Popcorn是一种基于RESTful API的通信协议,该协议允许请求客户端标识检索资源或资源集合时要包括的资源的各个字段。

It allows for a recursive selection of fields, allowing multiple calls to be condensed into one. 它允许递归选择字段,从而将多个调用压缩为一个。

Features: 特征:

  • Selective inclusion from a RESTful API 来自RESTful API的选择性包含
  • Configurable response defaults 可配置的响应默认值
  • Sorting of responses 回应排序
  • Selective authorization and permissioning of response values 选择性授权和允许响应值
  • Configurable response inspectors 可配置的响应检查器
  • Factory and advanced projection support 工厂和高级投影支持
  • Set relevant contexts for your API 为您的API设置相关的上下文
  • Blind expansion f response objects 盲目扩展f响应对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM