C# 来自ADO.Net模型的Json响应

C# 来自ADO.Net模型的Json响应,c#,.net,ado.net,nancy,C#,.net,Ado.net,Nancy,我有一个带有ADO.Net模型的Nancy应用程序,我想返回一些对象,如: Get["/getById/{id:int}"] = _ => { int id = _.id; User user = EntityContext.Users.Find(id); if (user != null) { r

我有一个带有ADO.Net模型的Nancy应用程序,我想返回一些对象,如:

 Get["/getById/{id:int}"] = _ =>
            {
                int id = _.id;
                User user = EntityContext.Users.Find(id);
                if (user != null)
                {
                    return Response.AsJson(user);
                }
                return "User not found with Id: " + id;
            };
生成的User.cs如下所示:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace NancyApplication1.Models
{
    using System;
    using System.Collections.Generic;

    public partial class User
    {
        public int id { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        public string email { get; set; }
        public string password { get; set; }
        public string profile_image { get; set; }
        public int type { get; set; }
        public string username { get; set; }

        public virtual UserType user_type { get; set; }
    }
}
//------------------------------------------------------------------------------
// 
//此代码是从模板生成的。
//
//手动更改此文件可能会导致应用程序出现意外行为。
//如果重新生成代码,将覆盖对此文件的手动更改。
// 
//------------------------------------------------------------------------------
名称空间NancyApplication1.Models
{
使用制度;
使用System.Collections.Generic;
公共部分类用户
{
公共int id{get;set;}
公共字符串名{get;set;}
公共字符串lastname{get;set;}
公共字符串电子邮件{get;set;}
公共字符串密码{get;set;}
公共字符串配置文件\u图像{get;set;}
公共int类型{get;set;}
公共字符串用户名{get;set;}
公共虚拟用户类型用户类型{get;set;}
}
}
问题是这段代码生成了一个空的响应体,你知道怎么了吗


提前谢谢你

在我看来,最好的选择是使用ApiModel类作为API响应

创建类(您可能也不希望在API中返回密码):

获取库,在应用程序引导程序中配置它:

Mapper.CreateMap<User, UserApiModel>();

就够了

我们还需要一点时间。什么类型的用户?您的路线和路线处理程序看起来如何?如何测试?编辑了问题:)停止直接从实体框架返回模型。Arg.那么我应该返回什么,@Phill?一个响应对象或视图模型。
Mapper.CreateMap<User, UserApiModel>();
var model = Mapper.Map<UserApiModel>(user);
return model;