Servicestack 访问 json [英] Servicestack accessing json

查看:55
本文介绍了Servicestack 访问 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Servicestack 服务正在发布 Json(通过 jquery).

My Servicestack service is beeing posted Json (by jquery).

sendData = function(dataToSend) {
  var request;
  return request = $.ajax({
    url: "/api/test",
    type: "post",
    data: JSON.stringify(dataToSend),
    dataType: "json",
    accept: "application/json",
    contentType: "application/json"
  });

如果 Json 数据对应于我的 DTO 的根属性(例如:userId:'foo' -> DTO 中的 UserId=foo),它就会被正确反序列化.

The Json data is correctly deserialized if it corresponds to the root properties of my DTO (eg: userId:'foo' -> UserId=foo in the DTO).

我想在反序列化之前访问原始 json 发布的数据以添加自定义反序列化.到目前为止,我在使用自定义过滤器(RequestFilterAttribute)访问查询字符串或者数据是否像表单一样发布时没有问题.现在我看到使用 Chrome 开发者工具发布的数据在带有Request Payload"的标题中,所以它既不在 FormData 中,也不在我调试 IHttpRequest 时可以访问的 QueryString 中.如何在过滤器中获取我的原始 json 数据?

I want to access the raw json posted data before it gets deserialized for adding custom deserialization. Till now I had no problems accessing the querystrings with custom filters (RequestFilterAttribute) or if data vas posted like form. Now I see the data that gets posted with Chrome Developer Tools is in the headers with "Request Payload" so it is nor in FormData and nor QueryString I can access when debugging my IHttpRequest. How can I get my raw json data in the filter?

推荐答案

如果您想用特定请求 DTO 的自定义行为替换默认反序列化行为,您可以在 AppHost 设置代码中执行此操作:

If you want to replace the default deserialization behavior with custom behavior for a specific request DTO, you can do this in your AppHost setup code:

JsConfig<MyRequestDtoClass>.DeSerializeFn = DeserializeMyRequestDto;

其中 DeserializeMyRequestDto 是一个函数或 lambda,它采用单个字符串参数(原始请求正文)并返回 DTO 的反序列化实例:

Where DeserializeMyRequestDto is a function or lambda taking a single string param - the raw request body - and returning the deserialized instance of your DTO:

MyRequestDtoClass DeserializeMyRequestDto(string rawBody) { ... }

RequestFilterAttribute 子类据称可以使用 request.GetRawBody() 访问原始请求正文,其中 request 是传递到过滤器的 Execute 方法.但根据我的经验,GetRawBody 返回一个空字符串,因为由于反序列化,那个时候请求输入流似乎已经被消耗掉了.我通过创建一个 HTTP 模块(通过 DynamicModuleUtility.RegisterModule 在 AppHost 中注册)解决了这个问题,该模块将查看"BeginRequest 事件处理程序中的请求输入流.(peek"函数会读取请求输入流,然后将流的 Position 重置为它最初所在的位置.)

RequestFilterAttribute subclasses purpotedly have access to the raw request body using request.GetRawBody(), where request is the IHttpRequest object passed into the filter's Execute method. But in my experience, GetRawBody returns an empty string, because the request input stream seems to be consumed by that time due to the deserialization. I worked around this once by creating an HTTP module (registered in AppHost via DynamicModuleUtility.RegisterModule) that would "peek" at the request input stream in a BeginRequest event handler. (The "peek" function would read the request input stream and then reset the Position of the stream to where it initially was.)

这篇关于Servicestack 访问 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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