How do I add properties to a Web API Response that I do not store in my DB?(如何向未存储在数据库中的 Web API 响应添加属性?)
问题描述
我正在使用 Entity Framework 6.0 构建一个 C# Web API.我有一个最简单的用户类,它有 3 个属性,我在 SQL 上坚持到一个用户表中,其中用户 ID 是它的主键.
I am building a C# Web API using Entity Framework 6.0. I have the simplest User Class with 3 properties that I persist on SQL into a User Table with 3 corresponding columns where UserID is its the Primary Key.
public partial class User
{
public string UserID {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
}
我想动态地向 Web API 添加两个仅输出属性,我不想将它们存储在我的数据库中.我使用这些属性与不属于用户类的消费客户端状态"和消息"信息进行通信.状态 = 正常|错误|警告.消息可以是 Web API 需要与调用客户端进行通信的任何消息.
I want to add to the Web API two output-only properties on the fly that I do not care to store in my DB. I use these properties to communicate to the consuming client "Status" and "Message" information that are not part of the User Class. Status = OK|Error|Warning. Message would be any message the Web API needs to communicate back to the calling client.
我的问题是:在发送回 Web API 的响应而不修改 SQL 上的基础用户表时,动态添加这两个属性的最简单方法是什么?我知道我可以将这两个作为虚拟列添加到用户表中.当我不需要它时,我不想在 SQL 端进行开销.
My question is: what is the simplest way to add these two properties on the fly upon sending back the Web API's response WITHOUT modifying the underlying User Table on SQL? I know I can add these two as dummy columns to the User Table. I don't want to carry around that overhead on the SQL side when I don't need it there.
推荐答案
我会采用更通用的方法:
I would go with more generic approach:
public class MyResponse<T>
{
public T Data {get;set;}
public Status ResponseStatus{get;set;}
public string Message{get;set;}
}
通过这种方式,您可以以相同的方式处理所有模型/数据.
This way you can handle all you models/data in the same way.
更新
[AllowAnonymous]
[RoutePrefix("api/home")]
public class HomeController : ApiController
{
[HttpGet]
[Route("ok")]
public MyResponse<MyUser> OK()
{
MyUser m = new MyUser();
var r = MyResponse<MyUser>.Success(m);
return r;
}
[Route("nok")]
[HttpGet]
public MyResponse<MyUser> NOK()
{
var r = MyResponse<MyUser>.Error("something went terribly wrong");
return r;
}
}
public class MyResponse<T>
{
public T Data { get; set; }
public Status ResponseStatus { get; set; }
public string Message { get; set; }
private MyResponse() { }
public static MyResponse<T> Success(T data)
{
return new MyResponse<T> { Data = data, ResponseStatus = Status.Success };
}
public static MyResponse<T> Error(string message)
{
return new MyResponse<T> { ResponseStatus = Status.Error, Message = message };
}
}
public class MyUser
{
public int Id { get; set; }
public string Name { get; set; }
}
public enum Status
{
Unknown = 0,
Success = 1,
Error
}
这篇关于如何向未存储在数据库中的 Web API 响应添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何向未存储在数据库中的 Web API 响应添加属性?
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01