ASP.NET Web API将MV响应转换为MVC 4中的json List

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET Web API将MV响应转换为MVC 4中的json List相关的知识,希望对你有一定的参考价值。

我正在尝试返回类别列表,但只返回表的第一行。

var categories = response.Content.ReadAsAsync<IEnumerable<Categories>>().Result;

变量类别返回:

[0] = Control.Models.Categories 
[1] = null 
[2] = null 
[3] = null

我的控制器:

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["Url"]);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var id = Session["UserId"];
var url = "api/categoria/" + id;
HttpResponseMessage response = client.GetAsync(url).Result;
var categories = response.Content.ReadAsAsync<IEnumerable<Categories>>().Result;

模型:

    [Key]
    public int intCategoriaPaiId { get; set; }

    [Key]
    public int intCategoriaOrdem { get; set; }

    [ForeignKey("intUsuarioId")]
    public virtual Usuario Usuario { get; set; }

    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int intCategoriaId { get; set; }

    public string strCategoriaNome { get; set; }

Controller WEB API:

    // GET api/categories/1
    public IEnumerable<Categories> Get(int id)
    {
        var categories = repository.GetCategoriesByintUsuarioId(id);

        if (categories == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }
        else
        {
            return categories;
        }
    }

JSON:

  [{"$id":"1","intCategoriaPaiId":0,"intCategoriaOrdem":1,"intUsuarioId":1,"intCategoriaId":2,"strCategoriaNome":"Receitas","Usuario":{"$id":"2","intUsuarioId":1,"strUsuarioNome":"Jose Henrique","strUsuarioEmail":"jose.costa@hotmail.com","strUsuariosenha":"KyPMH!ag","Categorias":[{"$ref":"1"},{"$id":"3","intCategoriaPaiId":2,"intCategoriaOrdem":1,"intUsuarioId":1,"intCategoriaId":3,"strCategoriaNome":"Salario","Usuario":{"$ref":"2"},"EntityKey":{"$id":"4","EntitySetName":"Categorias","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intCategoriaPaiId","Type":"System.Int32","Value":"2"},{"Key":"intCategoriaOrdem","Type":"System.Int32","Value":"1"},{"Key":"intUsuarioId","Type":"System.Int32","Value":"1"}]}},{"$id":"5","intCategoriaPaiId":3,"intCategoriaOrdem":1,"intUsuarioId":1,"intCategoriaId":4,"strCategoriaNome":"TESTE","Usuario":{"$ref":"2"},"EntityKey":{"$id":"6","EntitySetName":"Categorias","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intCategoriaPaiId","Type":"System.Int32","Value":"3"},{"Key":"intCategoriaOrdem","Type":"System.Int32","Value":"1"},{"Key":"intUsuarioId","Type":"System.Int32","Value":"1"}]}},{"$id":"7","intCategoriaPaiId":3,"intCategoriaOrdem":2,"intUsuarioId":1,"intCategoriaId":8,"strCategoriaNome":"Freela","Usuario":{"$ref":"2"},"EntityKey":{"$id":"8","EntitySetName":"Categorias","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intCategoriaPaiId","Type":"System.Int32","Value":"3"},{"Key":"intCategoriaOrdem","Type":"System.Int32","Value":"2"},{"Key":"intUsuarioId","Type":"System.Int32","Value":"1"}]}}],"EntityKey":{"$id":"9","EntitySetName":"Usuarios","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intUsuarioId","Type":"System.Int32","Value":"1"}]}},"EntityKey":{"$id":"10","EntitySetName":"Categorias","EntityContainerName":"DatabaseContext","EntityKeyValues":[{"Key":"intCategoriaPaiId","Type":"System.Int32","Value":"0"},{"Key":"intCategoriaOrdem","Type":"System.Int32","Value":"1"},{"Key":"intUsuarioId","Type":"System.Int32","Value":"1"}]}},{"$ref":"3"},{"$ref":"5"},{"$ref":"7"}]
答案

我解决了这个问题。我的模特错了

型号分类:

[Key]
public int intCategoriaPaiId { get; set; }

[Key]
public int intCategoriaOrdem { get; set; }

[ForeignKey("Usuario")]
public int intUsuarioId { get; set; }
public Usuario Usuario { get; set; }

[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int intCategoriaId { get; set; }

public string strCategoriaNome { get; set; }

模型用户缺乏:

public virtual ICollection<Categoria> categorias { get; set; }

以上是关于ASP.NET Web API将MV响应转换为MVC 4中的json List的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET Web API 2 的返回结果

ASP.NET MVC4 Web API MediaTypeFormatter 转换器将 XElement 转换为 JSON

如何将 .aspx 页面中的 ASP.NET Web 方法转换为 RESTful API?

如何在 ASP.Net MVC4 Web API 项目中使用 Microsoft OCR 库 ( Microsoft.Windows.Ocr )?

ASP.NET Odata Web API 的错误处理

如何在 ASP Net Core Web API 上正确地将列表转换为 IQueryable