ModelBinding:POST数据(可能来自红宝石)在MVC4 / C# [英] ModelBinding: POST data (possibly from Ruby) in MVC4/C#

查看:254
本文介绍了ModelBinding:POST数据(可能来自红宝石)在MVC4 / C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在与chargify http://www.chargify.com 整合和我需要处理来自网络挂接Chargify我们MVC4 / C#服务器。
Chargify的(红宝石)的方式发送POST数据 - 子对象分隔在方括号中,像这样:

We're integrating with chargify http://www.chargify.com and I need to handle webhooks from Chargify in our MVC4/C# server. Chargify sends POST data in the (ruby) way - sub objects are delimited in square brackets, like so:

POST /1ffaj2f1 HTTP/1.1
X-Chargify-Webhook-Signature: 526ccfd9677668674eaa6ba5d447e93a
X-Chargify-Webhook-Id: 11238622
User-Agent: Ruby
Host: requestb.in
Content-Type: application/x-www-form-urlencoded
Content-Length: 5159
Connection: close
Accept-Encoding: gzip, deflate
Accept: */*; q=0.5, application/xml

id=11238622&event=payment_success&payload[subscription][activated_at]=2013-05-22%2001%3A25%3A20%20-0400&payload[subscription][balance_in_cents]=0&...

问题是,在.NET方面,默认模型联编不喜欢的方括号 [ ] ,而是希望子对象,以点分符号分隔像 ABCD =值

The issue is that on the .NET side, the default model binder does not like the square brackets[ ] and instead expects subobjects to be delimited in a dotted notation like a.b.c.d=value

什么是我的选择,使模型绑定工作?我能想到如下:

What are my options to make the model binder work? I can think of the following:


  1. 勾成一个委托处理程序的要求,阅读REQ身体和圆点代替[](。 )。然而,这种感觉在最少

  2. 冒险编写自定义模型粘结剂,将与合作 [] - 希望能够委托给不知怎的,默认的处理程序。不知道如何去了解这一点 - 但很想听到一些想法

  3. 只需在操作使用的FormCollection 参数。处理表单收集钥匙,形成基于密钥的递归字典。这也意味着,我必须处理整个室内用串(日期,IDS等等等等)

  1. Hook into the request with a delegating handler, read the req body and replace [] with dot (.). However, this feels dicey at the least
  2. Write a custom model binder that will work with [] - hopefully being able to delegate to the default handler somehow. Dunno how to go about this - but would love to hear some thoughts.
  3. Just use a formcollection param in the Action. Process the form collection keys to form a recursive dictionary based on the keys. This also means that I have to deal wiht strings throughout (for dates, ids etc etc)

什么是最好的方式我该怎么办去了解它。

What's the best way and how do I go about it.

推荐答案

所以,我想我终于解决了这一使用 Request.Filter (MSDN)

So I think I've finally solved this one using Request.Filter property (msdn)

另外要注意的 - 有没有在MVC没有委托处理程序 - 这是的WebAPI的东西(其中的一些东西都做只是为了迷惑你!)

Also to note - There's no delegating handler in MVC - that's WebApi stuff (some of these things are made just to lead you astray!)

基本上,写了流的装饰,读取输入流中另一个流替换方括号点,然后将其存储。
有一个在一个的Application_BeginRequest检查看是否输入请求是正确的控制器,如果是这样,创建一个实例,装饰,它设置为Request.Filter。

Basically, wrote a decorator over Stream that reads the input stream, replaces the square brackets with dots and then stores it in another stream. There's a check in Application_BeginRequest that sees if the input request is for the 'correct' controller and if so, creates a decorator instance and sets it to Request.Filter.

这里。随意叉:)

[更新 - 2013年8月19日]::由@benpage下面的评论指出的那样,我们还需要原未修改流为SHA256验证。
我结束了从装饰暴露未修改的流内容和向下转换控制器中的 Request.InputStream 属性来获取签名验证未修改的内容。

[Update - 2013.08.19]: As noted by @benpage below in comments, we also need the original unmodified stream for SHA256 validation. I ended up exposing the unmodified stream contents from the decorator and downcasting the Request.InputStream property in the controller to get the unmodified content for signature validation.

这篇关于ModelBinding:POST数据(可能来自红宝石)在MVC4 / C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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