I'm not quite sure how to ask this question. I actually had this working before, and then I made some seemingly trivial changes and broke it, and I'm not sure how to make it work.
我不太确定如何提出这个问题。我之前实际上有这个工作,然后我做了一些看似微不足道的变化并打破了它,我不知道如何让它工作。
What I want to do is have a dynamic object parameter in my controller action, so that when some serialized form data is posted to it, the dynamic object will automatically bind to the various properties sent to it.
我想要做的是在我的控制器操作中有一个动态对象参数,这样当一些序列化表单数据发布到它时,动态对象将自动绑定到发送给它的各种属性。
I've looked around at a bunch of questions, but most were old and indicated that a custom model binder was needed, but I would like to reiterate that this was working without a custom model binder. I was just including a formData in my post data and catching it with a dynamic formData parameter.
我已经查看了一堆问题,但大多数都是旧的,并表示需要自定义模型绑定器,但我想重申,这是在没有自定义模型绑定器的情况下工作。我只是在我的帖子数据中包含一个formData,并用动态的formData参数捕获它。
So under what conditions does ASP.NET assume this is what I want?
那么ASP.NET在什么条件下假设这就是我想要的呢?
2 个解决方案
#1
public void Post(JObject dynamicJSON)
{
dynamic myObj = dynamicJSON;
int id = myObj.Id
}
JObject class is part of Newtonsoft.JSON library
JObject类是Newtonsoft.JSON库的一部分
#2
i was goggling one day i see that threat..its working to
有一天,我看到了那种威胁,我正在努力工作
controller
[HttpPost]
public ActionResult DoSomething(string a, string b, dynamic c)
{
return new EmptyResult();
}
Then do it dynamic model binder interface declaration
然后做动态模型绑定器接口声明
public class DynamicJsonAttribute : CustomModelBinderAttribute
{
public override IModelBinder GetBinder()
{
return new DynamicJsonBinder(MatchName);
}
public bool MatchName { get; set; }
}
and here is implementation
这是实施
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Web.Helpers;
using System.Web.Mvc;
public class DynamicJsonBinder : IModelBinder
{
private readonly bool matchName;
public DynamicJsonBinder(bool matchName)
{
this.matchName = matchName;
}
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var contentType = controllerContext.HttpContext.Request.ContentType;
if (!contentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
return null;
string bodyText;
using (var stream = controllerContext.HttpContext.Request.InputStream)
{
stream.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(stream))
bodyText = reader.ReadToEnd();
}
if (string.IsNullOrEmpty(bodyText)) return null;
var desiralized = Json.Decode(bodyText);
if (!matchName) return desiralized;
var members = desiralized.GetDynamicMemberNames() as IEnumerable<string>;
return members == null
|| members.Contains(bindingContext.ModelName)
? desiralized[bindingContext.ModelName] : null;
}
}
you ve been hacked ---happy coding
你被黑了 - 很开心的编码
anyway i was read the thread from this url---look it out
无论如何,我从这个网址读取了线程---看看它
#1
public void Post(JObject dynamicJSON)
{
dynamic myObj = dynamicJSON;
int id = myObj.Id
}
JObject class is part of Newtonsoft.JSON library
JObject类是Newtonsoft.JSON库的一部分
#2
i was goggling one day i see that threat..its working to
有一天,我看到了那种威胁,我正在努力工作
controller
[HttpPost]
public ActionResult DoSomething(string a, string b, dynamic c)
{
return new EmptyResult();
}
Then do it dynamic model binder interface declaration
然后做动态模型绑定器接口声明
public class DynamicJsonAttribute : CustomModelBinderAttribute
{
public override IModelBinder GetBinder()
{
return new DynamicJsonBinder(MatchName);
}
public bool MatchName { get; set; }
}
and here is implementation
这是实施
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Web.Helpers;
using System.Web.Mvc;
public class DynamicJsonBinder : IModelBinder
{
private readonly bool matchName;
public DynamicJsonBinder(bool matchName)
{
this.matchName = matchName;
}
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var contentType = controllerContext.HttpContext.Request.ContentType;
if (!contentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
return null;
string bodyText;
using (var stream = controllerContext.HttpContext.Request.InputStream)
{
stream.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(stream))
bodyText = reader.ReadToEnd();
}
if (string.IsNullOrEmpty(bodyText)) return null;
var desiralized = Json.Decode(bodyText);
if (!matchName) return desiralized;
var members = desiralized.GetDynamicMemberNames() as IEnumerable<string>;
return members == null
|| members.Contains(bindingContext.ModelName)
? desiralized[bindingContext.ModelName] : null;
}
}
you ve been hacked ---happy coding
你被黑了 - 很开心的编码
anyway i was read the thread from this url---look it out
无论如何,我从这个网址读取了线程---看看它