ASP.net的Web API响应 - 循环数据 [英] ASP.net Web api response - looping data

查看:177
本文介绍了ASP.net的Web API响应 - 循环数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

荫使用ADO.net在我的MVC应用5我ORM工具,问题是:

Iam using ADO.net as my ORM tool in my MVC 5 app, and the problem is :


  • 当我做出一些POST操作到我的网页API和返回一些数据,我响应JSON具有forign键的所有值,但我没有包括一些数据。

看一下这个简单的行:

    public async Task<IHttpActionResult> GetMyMessage(int id)
    {

        List<Messages> messages = await db.Messages.Where(a => a.GroupID == id).ToListAsync();


        return Ok(messages);

    }

问题是在这里,我没有任何inclued数据(例如db.Messages.Include(东西...))到我的邮件,但还是老样子响应我得到例如,所有用户的集合,和其他的东西,我的消息表与在DB连接。

The problem is here that I didn't inclued any data(e.g. db.Messages.Include(something...)) to my messages but stil in response i got e.g all Users collections, and other stuff that my message table is connected with in DB.

我ASAX全球是:

protected void Application_Start()
    {

        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

    }

有谁知道如何摆脱关闭此链接表和有干净的JSON响应。否则我将不得不做一些变通方法,并进行清洁了一下。 ?

Does anyone Know how to get rid off this linked tables and have clean json response. Otherwise i would have to do some workaround and clean it a bit. ?

推荐答案

这听起来像一个的ViewModels情况。而不是返回你的的消息实体您可以创建一个新的类型,只有你关心的是结果的属性。说你的实体如下:

This sounds like a case for ViewModels. Instead of returning your Message entity you can create a new type with only the properties you care about for that result. Say your entities look like:

public class Message {
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual User User { get; set; }
    public virtual Group Group { get; set; }
}

public class User {
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Message> Messages { get; set; }
}

public class Group {
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Message> Messages { get; set; }
}

和你有一个由组ID检索消息,但不希望有关用户或。创建一个视图模型重新present你想要的实际响应:

And you have an endpoint that retrieves a Message by the Group Id but does not want any info about the User or Group. Create a ViewModel to represent the actual response you want:

public class MessageViewModel {
    public int Id { get; set; }
    public string Name { get; set; }
}

和则该实体转换为视图模型:

And then convert the entity to the ViewModel:

public async Task<IHttpActionResult> GetMyMessage(int id)
{
    var messages = await db.Messages.Where(a => a.Group.Id == id).ToListAsync();
    var models = messages.Select(
        m => new MessageViewModel {
            Id = m.Id, 
            Name = m.Name
        }).ToList();

    return Ok(models);
}

现在像返回以下内容:

And now something like the following is returned:

[{id: 1, name: 'SomeMessageName'}, {id: 2, name: 'SomeOtherMessageName'}, ...]

这篇关于ASP.net的Web API响应 - 循环数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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