从返回的WebAPI型JsonNet在JSON果园 [英] Return JsonNet type JSON from WebAPI in Orchard

查看:297
本文介绍了从返回的WebAPI型JsonNet在JSON果园的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标准的MVC我用JsonNet返回JSON是在驼峰并成功地序列化具有相关实体的实体(否则报告一个循环错误使用默认的序列化)。

In standard MVC I use JsonNet to return JSON that is in camelCase and sucessfully serializes entities that have related entities (which otherwise reports a "cycles" error" using the default serializer).

我想一个果园模块中的WebAPI控制器这样做。默认情况下它返回PascalCase JSON和有关实体的名单时,报告了个循环的异常。

I'd like to do the same for a WebAPI controller in an Orchard module. By default it returns PascalCase JSON and reports a "cyles" exception when given a list of entities.

谁能解释如何最好地从果园模块内配置JSON输出,以模仿什么JsonNet会产生?

Can anyone explain how best to configure the JSON output from within the Orchard module, to mimic what JsonNet would produce?

推荐答案

我已经找到了解决办法,这是设置在ActionFilter的JSON格式化设置驼峰:

I've found a workaround, which is to set the JSON formatters settings to camelCase in an ActionFilter:

    public class CamelCaseJsonAttribute : ActionFilterAttribute {

    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        var jsonFormatter = actionContext.ControllerContext.Configuration.Formatters.OfType<JsonMediaTypeFormatter>().First();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    }
}

但这似乎效率不高,因为它得到对每个请求设置,而不是在全球范围一次。

But this seems inefficient, as it gets set on each request, rather than globally, once.

我在一个模块中的某处猜测有一个扩展点设置HttpConfiguration - 谁能告诉我一个方法或其他

I'm guessing there is an extensibility point somewhere in a module to set the HttpConfiguration - can anyone tell me one way or the other?

非常感谢。

这篇关于从返回的WebAPI型JsonNet在JSON果园的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆