简体   繁体   English

ASP.Net MVC:向控制器发送JSON

[英]ASP.Net MVC : Sending JSON to Controller

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. 我希望能够在ASP.Net MVC中向我的控制器发布帖子时发送JSON而不是标准的QueryStrings。 I have the Front-End stuff working fine (building and then submitting my JSON objects). 我有前端工作正常(构建然后提交我的JSON对象)。

The problem is on the controller side where the default ModelBinders that ship with the MVC framework do not support this. 问题出在控制器端,MVC框架附带的默认ModelBinder不支持此功能。

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. 我已经看到了各种方法,其中之一是应用一个过滤器,它将对象作为参数,使用JSON库对其进行反序列化,并将其添加到操作参数中。 This is not ideal. 这不太理想。

The other, better, way is to use a custom Model Binder. 另一种更好的方法是使用自定义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. 我有一个想法,如果我可以简单地覆盖MVC如何处理FormCollection并拦截那里,我自己将值添加到集合中,并希望MVC能够以正常的方式完成剩下的工作。 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. 问题是从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. 如果我是正确的MVC获取QueryString中的值并将其放入表单集合中,然后将其用于ModelBinding。 So shouldn't the correct method be to change the way the FormCollection gets assigned? 那么正确的方法是不应该改变FormCollection的分配方式?

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. 正常的绑定工作,JSON没有,模型绑定器的所有示例都不起作用。 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. 看起来不太理想。

I use a custom model binder for json like this: 我为json使用自定义模型绑定器,如下所示:

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);
    }
}

And then wire it up in Global.asax.cs like this: 然后将它连接到Global.asax.cs中,如下所示:

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

You can read more about this here: Inheritance is Evil: The Epic Fail of the DataAnnotationsModelBinder 你可以在这里阅读更多相关内容: 继承是邪恶的:DataAnnotationsModelBinder的Epic Fail

EDIT 编辑

The JsonModelBinder should be used on the controller action parameter typed as Product only. 应将JsonModelBinder用于仅作为Product键入的控制器操作参数。 The Int32 and ClassObject should fall back to the DefaultModelBinder. Int32和ClassObject应该回退到DefaultModelBinder。 Are you experiencing a different result? 您是否遇到了不同的结果?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 淘汰赛和Asp.net MVC发送json - Knockout and Asp.net MVC sending json 将mson发送到控制器时,ASP.NET mvc 4控制器参数始终为null,为什么? - ASP.NET mvc 4 controller parameter always null when sending json to controller, why? 将json发送到asp.net mvc控制器确实可以创建对象,但不能填充属性 - Sending json to asp.net mvc controller does make objects, but doesn't fill properties 将 JSON 从视图发送到控制器 ASP.NET MVC 给出空值 - Sending JSON from View to Controller ASP.NET MVC gives null values 在将数据发送到View之前,附加到ASP.NET中MVC控制器中的Json对象 - Append to a Json object in MVC controller in ASP.NET before sending data to the View 无法将 JSON 数据发送到 asp.net MVC controller 中的另一个操作 - Trouble sending JSON data to another action in asp.net MVC controller 将Javascript数组发送到控制器ASP.NET MVC - Sending a Javascript array to controller ASP.NET MVC 将URL /路径发送到ASP.NET MVC控制器操作 - Sending URLs/paths to ASP.NET MVC controller actions 选定的DropDownList值未发送到ASP.NET MVC控制器 - Selected DropDownList Value Not Sending to ASP.NET MVC Controller ASP.NET MVC 代码模式弹出不发送参数到 controller - ASP.NET MVC code modal popup not sending parameter to controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM