C# 使用MVC4返回值的正确方法

C# 使用MVC4返回值的正确方法,c#,asp.net-mvc-4,asp.net-web-api,C#,Asp.net Mvc 4,Asp.net Web Api,在MVC4中,通过API控制器返回JSON数据的正确方法是什么?我听说您需要使用变量类型作为函数,但是我不能这样做,因为我不能使用 我所做的是像这样使用dynamic [HttpGet] public dynamic List() // Not List<Item> { var items = _db.Items.OrderBy(x => x.ID).Select(x => new { ID = x.ID, Title =

在MVC4中,通过API控制器返回JSON数据的正确方法是什么?我听说您需要使用变量类型作为函数,但是我不能这样做,因为我不能使用

我所做的是像这样使用
dynamic

[HttpGet]
public dynamic List() // Not List<Item>
{
    var items = _db.Items.OrderBy(x => x.ID).Select(x => new
    {
        ID = x.ID,
        Title = x.Title,
        Price = x.Price,
        Category = new {
            ID = x.Category.ID,
            Name = x.Category.Name
        }
    });

    return items;
}
[HttpGet]
公共动态列表()//非列表
{
var items=_db.items.OrderBy(x=>x.ID)。选择(x=>new
{
ID=x.ID,
Title=x.Title,
价格=x.价格,
类别=新{
ID=x.Category.ID,
Name=x.Category.Name
}
});
退货项目;
}

这是最好的方法吗?我这么问是因为我刚开始使用MVC4,我不想过早养成坏习惯:)

内置函数
Controller.Json
()可以做你想做的事情,即假设你的代码驻留在Controller类中:

[HttpGet]
public dynamic List() // Not List<Item>
{
    var items = _db.Items.OrderBy(x => x.ID).Select(x => new
    {
        ID = x.ID,
        Title = x.Title,
        Price = x.Price,
        Category = new {
            ID = x.Category.ID,
            Name = x.Category.Name
        }
    });

    return Json(items, JsonRequestBehavior.AllowGet);
}
[HttpGet]
公共动态列表()//非列表
{
var items=_db.items.OrderBy(x=>x.ID)。选择(x=>new
{
ID=x.ID,
Title=x.Title,
价格=x.价格,
类别=新{
ID=x.Category.ID,
Name=x.Category.Name
}
});
返回Json(items,JsonRequestBehavior.AllowGet);
}

如果您想在GET请求中使用它,那么应该使用重载,它接受
JsonRequestBehavior
标志作为参数,并为该参数指定
JsonRequestBehavior.AllowGet

内置函数
Controller.Json
()可以执行您想要的操作,即假设您的代码驻留在控制器类中:

[HttpGet]
public dynamic List() // Not List<Item>
{
    var items = _db.Items.OrderBy(x => x.ID).Select(x => new
    {
        ID = x.ID,
        Title = x.Title,
        Price = x.Price,
        Category = new {
            ID = x.Category.ID,
            Name = x.Category.Name
        }
    });

    return Json(items, JsonRequestBehavior.AllowGet);
}
[HttpGet]
公共动态列表()//非列表
{
var items=_db.items.OrderBy(x=>x.ID)。选择(x=>new
{
ID=x.ID,
Title=x.Title,
价格=x.价格,
类别=新{
ID=x.Category.ID,
Name=x.Category.Name
}
});
返回Json(items,JsonRequestBehavior.AllowGet);
}

如果您想在GET请求中使用它,那么您应该使用重载,它接受
JsonRequestBehavior
标志作为参数,并为该参数指定
JsonRequestBehavior.AllowGet

您不需要使用
dynamic
,对于匿名类型,简单的方法是返回
object

[HttpGet] 
public object List() // Not List<Item> 
{ 
    var items = _db.Items.OrderBy(x => x.ID).Select(x => new 
    { 
        ID = x.ID, 
        Title = x.Title, 
        Price = x.Price, 
        Category = new { 
            ID = x.Category.ID, 
            Name = x.Category.Name 
        } 
    }); 

    return items; 
}

您不需要使用
动态
,简单的方法是为匿名类型返回
对象

[HttpGet] 
public object List() // Not List<Item> 
{ 
    var items = _db.Items.OrderBy(x => x.ID).Select(x => new 
    { 
        ID = x.ID, 
        Title = x.Title, 
        Price = x.Price, 
        Category = new { 
            ID = x.Category.ID, 
            Name = x.Category.Name 
        } 
    }); 

    return items; 
}