English 中文(简体)
如何返回 Api 控制器的多对象类型
原标题:How to return multiple object type for ApiControllers

我已经升级了我的项目 使用ASP.NET MVC4 从MVC3。 现在,我的控制器 使用Api 主计长 而不是 主计长。 以前我使用的功能是:

 public JsonResult GetPermissions(string portfolioId)
 {
     //DO THE DATA FETCH
     return Json(new { PermissionValues = permissionValues,       UserPermissions = userPermissions, OwnerValues = ownerList },JsonRequestBehavior.AllowGet);
 } 

我想使用 Api 控制器做类似的操作。 我不想创建一个单独的对象, 该对象将具有 3 个属性 [因为我正在返回 3 列表] 。 我试图使用 < code> HtpResponseMessage< Object> 作为函数的返回类型, 但似乎无效 。

    public HttpResponseMessage<Object> GetPermissions(string portfolioId)
    {
      //DO THE DATA FETCH
      HttpResponseMessage<Object> response = new HttpResponseMessage<Object>(new { Users = listedUsers, PermissionValues = permissionValues });
      return response;
    }

有什么想法吗?

最佳回答

您拥有的主要问题是, Beta 位元中 Json 响应的默认序列显示器无法序列匿名类型 。 您需要使用基于 Json. NET ( 将在下一滴中嵌入) 的备忘。 要在 Beta 上进行这项工作, 您的代码将会变成类似 :

public HttpResponseMessage Get()
{
    var content = new { Users = new List<string> { "John", "Mary" }, Permissions = new List<string> { "User", "Admin" } };
    return new HttpResponseMessage<object>(content, new[] { new JsonNetFormatter() });
}

这将产生这样的反应:

{"Users":["John","Mary"],"Permissions":["User","Admin"]}

一旦有驻地协调员,你现有的守则就应该正常工作。

更新 :

如果要编译此代码,您需要为 JsonNetFormatter 执行该代码。 您可以在或WebApiContrib项目

注意, 在 OnWriteToStreamAsync 方法中, 它们都遭受同样的错误。 这些执行关闭了底流 。 您需要更改使用过的语句才能看起来 :

using (var jsonTextWriter = new JsonTextWriter(new StreamWriter(stream, Encoding)) { CloseOutput = false })

Henrik在他的博客中注意到这一点,但他的样本和笔试都没有更新,而我只是没有时间对笔试项目进行修正。 )

问题回答

暂无回答




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签