I have get method, which should return users and there permissions. I have a users object and a permissions object, which i populate like this:

List<Users> users= MtFacade.GetUser(query);
List<UserPermissions> upermissions = MtFacade.GetUserPermission(User.Identity.Name);

My API controller returns a HttpResponse, for which I need to send back two lots of JSON, using the two objects.

I can do this if i return just one at a time, but how do i combined the two?

有帮助吗?

解决方案

Wrap them in a class that encompasses properties of their types.

I'm sure you've already figured this out since you've accepted the answer, but in case it helps you or others that see this question here is a code example:

// this would be your class that can encompass the two objects
public class UsersAndPermissions
{
    public List<User> Users { get; set; }
    public List<UserPermissions> Permissions { get; set; }
}

In your web api method use the above class like this

var usersAndPerms = new UsersAndPermissions
{
    Users = MtFacade.GetUser(query),
    Permissions = MtFacade.GetUserPermission(User.Identity.Name)
};

// now your json that you return from the web api has both of the objects data
return usersAndPerms;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top