Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 当我使用Web API从匿名类返回数据时,返回类型使用什么?_Asp.net Mvc_Asp.net Mvc 3_Asp.net Web Api - Fatal编程技术网

Asp.net mvc 当我使用Web API从匿名类返回数据时,返回类型使用什么?

Asp.net mvc 当我使用Web API从匿名类返回数据时,返回类型使用什么?,asp.net-mvc,asp.net-mvc-3,asp.net-web-api,Asp.net Mvc,Asp.net Mvc 3,Asp.net Web Api,我有以下ASP MVC4代码: [HttpGet] public virtual ActionResult GetTestAccounts(int applicationId) { var testAccounts = ( from testAccount in this._testAccountService.GetTestAccounts(3) select new

我有以下ASP MVC4代码:

    [HttpGet]
    public virtual ActionResult GetTestAccounts(int applicationId)
    {
        var testAccounts =
            (
                from testAccount in this._testAccountService.GetTestAccounts(3)
                select new
                {
                    Id = testAccount.TestAccountId,
                    Name = testAccount.Name
                }
            ).ToList();

        return Json(testAccounts, JsonRequestBehavior.AllowGet);
    }
现在我将其转换为使用Web API。有人能告诉我吗
如果我像这里一样返回匿名类,我的返回类型应该是什么?

它应该是
HttpResponseMessage

public class TestAccountsController: ApiController
{
    public HttpResponseMessage Get(int applicationId)
    {
        var testAccounts =
            (
                from testAccount in this._testAccountService.GetTestAccounts(3)
                select new 
                {
                    Id = testAccount.TestAccountId,
                    Name = testAccount.Name
                }
            ).ToList();

        return Request.CreateResponse(HttpStatusCode.OK, testAccounts);
    }
}
但是好的实践要求您应该使用视图模型(顺便说一句,您也应该在ASP.NET MVC应用程序中这样做):

然后:

public class TestAccountsController: ApiController
{
    public List<TestAccountViewModel> Get(int applicationId)
    {
        return
            (
                from testAccount in this._testAccountService.GetTestAccounts(3)
                select new TestAccountViewModel 
                {
                    Id = testAccount.TestAccountId,
                    Name = testAccount.Name
                }
            ).ToList();
    }
}
公共类TestAccountsController:ApicController
{
公共列表获取(int applicationId)
{
返回
(
来自此中的testAccount。\u testAccountService.GetTestAccounts(3)
选择NewTestAccountViewModel
{
Id=testAccount.TestAccountId,
Name=testAccount.Name
}
).ToList();
}
}

我会“包装”这个函数,让它返回一个强命名类型。当类型可序列化时,另一端可以解释该类型。通过包装,您不必编辑公开API的应用程序内部的代码。@PabloRomeo,是的,这正是它应该读取的方式。谢谢你发现了。我已经更新了我的答案。我同意,如果您要返回的类型不是
HttpResponseMessage
,那么它绝对应该是某种视图模型,但是我不同意这是返回
HttpResponseMessage
的最佳实践,这只是使用Web API的一种不同风格。
public class TestAccountsController: ApiController
{
    public List<TestAccountViewModel> Get(int applicationId)
    {
        return
            (
                from testAccount in this._testAccountService.GetTestAccounts(3)
                select new TestAccountViewModel 
                {
                    Id = testAccount.TestAccountId,
                    Name = testAccount.Name
                }
            ).ToList();
    }
}