从UserManager <applicationuser>返回整数作为Json

时间:2017-04-30 08:19:42

标签: jquery json asp.net-web-api

我正面临从asp.net web api(返回fron AspNetUsers表)返回此Id值作为Json。这可以将它作为Json返回,然后在Jquery中获取它吗?现在我得到零因为这可能不会像Json那样返回。提前谢谢!

public IHttpActionResult GetDepId()
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            string name = User.Identity.Name;

            var depId = manager.Users.Where(d => d.Email == name).Select(c => c.DepartmentId).FirstOrDefault();
            return Ok(depId);
        }

然后在jquery中:

function GetDept() {


            $.ajax({
                url: '/Api/userdp', // userdp is my controller
                type: 'GET',


                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: function (result) {
                    result; // failed even this result.Email; 
                },
                error:  {


                }
            });
        }

1 个答案:

答案 0 :(得分:0)

据我所知,你不能那样使用Ok()。请尝试在控制器中使用return new ObjectResult(depId);

编辑:是的,你可以。您只需要序列化输出,或手动生成JSON对象,如下所示:

return Ok(string.Format("id:{0}", depId.ToString()));