ASP.Net MVC:发送JSON到控制器 [英] ASP.Net MVC : Sending JSON to Controller

查看:124
本文介绍了ASP.Net MVC:发送JSON到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够发送JSON作为制作后到我在ASP.Net MVC控制器时,不是标准的查询字符串。我有前端的东西工作的罚款(建筑,然后提交我的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).

问题是在控制器一侧,与MVC框架自带的默认ModelBinders不支持这一点。

The problem is on the controller side where the default ModelBinders that ship with the MVC framework do not support this.

我见过的办法解决这个组合,其中一个是应用过滤器取对象作为参数,使用JSON库来取消它连载,并添加到动作参数。这是不理想的。

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.

另外,更好,方法是使用自定义的模型绑定。所有我见过,虽然presume那些你将只有一个模式,这将是一个类,而不是一个变量。如果你有多个的它打破了。

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.

有没有其他人遇到过吗?我有一个想法,如果是我可以简单地覆盖在的FormCollection和拦截哪有M​​VC交易,增加值来收集自己,希望MVC可以做其余的在它的正常方式。有谁知道,如果这是可能的?

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?

问题的关键,我想,是我的问题是不是与绑定,因为我的观点模型是他们以前怎么在这里没有什么不同。问题是从JSON后得到的数值。

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.

如果我是正确的MVC获得是从查询字符串值,并将其放入形式收集,然后用于ModelBinding。所以,不应该正确的方法是改变的FormCollection被分配的方式吗?

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?

一个动作的例子:

public ActionResult MyFirstAction(Int32 ID, PersonObject Person, ClassObject ClassDetails)
{
//etc
}

正常绑定工作,JSON不和模型绑定的所有示例将不能工作。我最好的解决办法,到目前为止是将对象转换为字典和循环尽管每个参数并匹配起来。似乎并不理想。

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.

推荐答案

我使用自定义的模型绑定像这样JSON:

I use a custom model binder for json like this:

public class JsonModelBinder<T> : IModelBinder {
    private string key;

    public JsonModelBinder(string requestKey) {
        this.key = requestKey;
    }

    public object BindModel(ControllerContext controllerContext, ...) {
        var json = controllerContext.HttpContext.Request[key];
        return new JsonSerializer().Deserialize<T>(json);
    }
}

再用线它在Global.asax.cs中是这样的:

And then wire it up in Global.asax.cs like this:

ModelBinders.Binders.Add(
    typeof(Product),
    new JsonModelBinder<Product>("ProductJson"));

您可以阅读更多关于此这里:<一href=\"http://www.agileatwork.com/inheritance-is-evil-the-story-of-the-epic-fail-of-dataannotationsmodelbinder/\">Inheritance是危机:DataAnnotationsModelBinder 史诗失败

You can read more about this here: Inheritance is Evil: The Epic Fail of the DataAnnotationsModelBinder

修改

该JsonModelBinder应类型,因为只有产品的控制器动作参数一起使用。该的Int32和ClassObject应该回落到DefaultModelBinder。您是否遇到一个不同的结果?

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?

这篇关于ASP.Net MVC:发送JSON到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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