如何为未存储在数据库中的Web API响应添加属性? [英] How do I add properties to a Web API Response that I do not store in my DB?

查看:111
本文介绍了如何为未存储在数据库中的Web API响应添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 6.0构建C#Web API.我拥有最简单的用户类,具有3个属性,可以在SQL上持久保存到具有3个相应列的用户表中,其中UserID是其主键.

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.我使用这些属性与不属于用户类的使用方客户端状态"和消息"信息进行通信.状态= OK |错误|警告.消息将是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.

我的问题是:在不修改SQL底层用户表的情况下,发送回Web API的响应时立即添加这两个属性的最简单方法是什么?我知道我可以将这两列作为虚拟列添加到用户表中.当我在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响应添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆