ASP.Net MVC:将 JSON 发送到控制器
在 ASP.Net MVC 中向我的控制器发布帖子时,我希望能够发送 JSON,而不是标准的查询字符串。我的前端工作正常(构建然后提交我的 JSON 对象)。
问题出在控制器端,MVC 框架附带的默认 ModelBinder 不支持此功能。
我已经看到了解决此问题的多种方法组合,其中之一是应用一个过滤器,该过滤器将对象作为参数,使用 JSON 库对其进行反序列化,并将其添加到操作参数中。这并不理想。
另一种更好的方法是使用自定义模型绑定器。我见过的所有模型都假设您只有一个模型,并且它将是一个类而不是一个变量。如果你有多个,它就会崩溃。
还有其他人遇到过这种情况吗?我的一个想法是,我是否可以简单地重写 MVC 处理 FormCollection 的方式并在那里进行拦截,自己将值添加到集合中,并希望 MVC 能够以正常方式完成其余的工作。有谁知道这是否可能?
我认为关键问题是我的问题不在于绑定,因为我的视图模型与以前没有什么不同。问题是从 JSON Post 获取值。
如果我是正确的,MVC 会从 QueryString 获取值并将其放入表单集合中,然后将其用于 ModelBinding。那么正确的方法不应该是改变 FormCollection 的分配方式吗?
操作示例:
public ActionResult MyFirstAction(Int32 ID, PersonObject Person, ClassObject ClassDetails)
{
//etc
}
普通绑定有效,JSON 无效,模型绑定器的所有示例也无效。到目前为止,我最好的解决方案是将对象转换为字典并循环每个参数并将其匹配。看起来不太理想。
I want to be able to send JSON as opposed to the standard QueryStrings when making a post to my controllers in ASP.Net MVC. I have the Front-End stuff working fine (building and then submitting my JSON objects).
The problem is on the controller side where the default ModelBinders that ship with the MVC framework do not support this.
I have seen a combination of ways around this, one of them is to apply a filter which takes the object as a parameter, uses a JSON library to de-serialise it, and adds that to the action parameters. This is not ideal.
The other, better, way is to use a custom Model Binder. All the ones I have seen though presume you will have only one model and that will be a class rather than a variable. If you have multiple ones it breaks down.
Has anyone else encountered this? One idea I had was if I could simply override how MVC deals with the FormCollection and intercept there, adding the values to the collection myself and hoping MVC can do the rest in it's normal fashion. Does anyone know if that is possible?
The key issue, I think, is that my problem is not with binding because my view models are no different to how they where before. The problem is getting the values from the JSON Post.
If I am correct MVC get's the values from the QueryString and puts it into the form collection which is then used for ModelBinding. So shouldn't the correct method be to change the way the FormCollection gets assigned?
Example of an action:
public ActionResult MyFirstAction(Int32 ID, PersonObject Person, ClassObject ClassDetails)
{
//etc
}
The normal binding works, JSON doesn't and all the example of Model Binders will not work either. My best solution so far is to convert the object to a dictionary and loop though each param and match it up. Doesn't seem ideal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 json 使用自定义模型绑定器,如下所示:
然后将其连接到 Global.asax.cs 中,如下所示:
您可以在此处阅读有关此内容的更多信息: 继承是邪恶的:DataAnnotationsModelBinder 的史诗般的失败
编辑 JsonModelBinder 应用于仅输入为 Product 的控制器操作参数。 Int32 和 ClassObject 应该回退到 DefaultModelBinder。您是否遇到了不同的结果?
I use a custom model binder for json like this:
And then wire it up in Global.asax.cs like this:
You can read more about this here: Inheritance is Evil: The Epic Fail of the DataAnnotationsModelBinder
EDIT
The JsonModelBinder should be used on the controller action parameter typed as Product only. The Int32 and ClassObject should fall back to the DefaultModelBinder. Are you experiencing a different result?