ASP.NET 4.5的WebAPI返回用户列表 [英] ASP.NET 4.5 WebAPI to return list of users

查看:172
本文介绍了ASP.NET 4.5的WebAPI返回用户列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来ASP.NET。就在ASP.NET MVC中,网络API和SPA完成了3本书。但它似乎不足以解决一个非常简单的问题。我有以下的code在我的web API控制器返回JSON / XML数据。所需的数据是返回与Identity框架注册的用户的列表。 MVC的,但不能找出网页API已经这样做了。

  [使用AllowAnonymous]
    〔路线(UserInfos)]
    公共IEnumerable的<&APPUSER GT; GetUserInfos()
    {
        清单<&APPUSER GT;登录=新的List<&APPUSER GT;();
        的foreach(APPUSER用户UserManager.Users)
        {            logins.Add(用户);
        }        返回的登录;    }

但我发现了错误,当我把这个网站的API。如下错误。

 <错误>
<消息方式>发生错误< /信息>
< ExceptionMessage>
在'ObjectContent`1类型没有序列化响应正文内容类型application / xml进行;字符集= UTF-8。
< / ExceptionMessage>
< ExceptionType> System.InvalidOperationException< / ExceptionType>
<堆栈跟踪/> ........
....
....

我在做一个用户经理/主管部门的SPA,SOI需要Web API返回的用户列表,角色等,就像MVC如何在模型/控制建设管理页面返回这些数据。

请帮忙。任何一个环节的教程是AP preciated,因为我已用尽一切seraching为这个问题的答案。谢谢。


下面这张code解决它。我创建了一个模型只是为了这个目的。但为什么我不能直接访问,并与身份模型工作?我能做到这一点的MVC。

  [使用AllowAnonymous]
    〔路线(UserInfos)]
    公共IEnumerable的< FlatUserModel> GetUserInfos()
    {
        清单< FlatUserModel>登录=新的List< FlatUserModel>();
        的foreach(APPUSER用户UserManager.Users)
        {
            logins.Add(新FlatUserModel
            {
                ID = user.Id,
                名称= user.UserName,
                电子邮件= user.Email            });
        }        返回的登录;
    }


解决方案

  [使用AllowAnonymous]
〔路线(UserInfos)]
公共IEnumerable的< FlatUserModel> GetUserInfos()
{
    清单< FlatUserModel>登录=新的List< FlatUserModel>();
    的foreach(APPUSER用户UserManager.Users)
    {
        logins.Add(新FlatUserModel
        {
            ID = user.Id,
            名称= user.UserName,
            电子邮件= user.Email        });
    }    返回的登录;
}

I'm new to ASP.NET. Just finished 3 books in ASP.NET MVC,Web API and SPA. But it seem not enough to solve a very simple problem. I Have the following code in my web api controller to return a JSON/XML data. The data required is to return a list of users registered with the Identity Framework. Have done this on MVC but not able to figure out the web api.

[AllowAnonymous]
    [Route("UserInfos")]
    public IEnumerable<AppUser> GetUserInfos()
    {
        List<AppUser> logins = new List<AppUser>();
        foreach (AppUser user in UserManager.Users)
        {

            logins.Add(user);
        }

        return logins;

    }

But I'm getting errors when I call this web API. Error as below.

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace/>........
....
....

I'm doing a user manager/administrations in an SPA, soI need the web api to return list of users, roles etc. Just like how MVC returns these data in the model/controlled to build the administration page.

please help. any link to tutorials is appreciated, as I have exhausted everything seraching for the answer to this. thanks.

[Edit] This code below solves it. I created a model just for this purpose. but why can't I directly access and work with the Identity model? I can do it in MVC.

[AllowAnonymous]
    [Route("UserInfos")]
    public IEnumerable<FlatUserModel> GetUserInfos()
    {
        List<FlatUserModel> logins = new List<FlatUserModel>();
        foreach (AppUser user in UserManager.Users)
        {
            logins.Add(new FlatUserModel
            {
                Id=user.Id,
                Name=user.UserName,
                Email=user.Email

            });
        }

        return logins;
    }

解决方案

[AllowAnonymous]
[Route("UserInfos")]
public IEnumerable<FlatUserModel> GetUserInfos()
{
    List<FlatUserModel> logins = new List<FlatUserModel>();
    foreach (AppUser user in UserManager.Users)
    {
        logins.Add(new FlatUserModel
        {
            Id=user.Id,
            Name=user.UserName,
            Email=user.Email

        });
    }

    return logins;
}

这篇关于ASP.NET 4.5的WebAPI返回用户列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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