编辑:
随着MVC 3的到来,不再需要这种方法,因为它将自动处理-http:
//weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3
-preview-1.aspx
您可以使用以下ObjectFilter:
public class ObjectFilter : ActionFilterAttribute { public string Param { get; set; } public Type RootType { get; set; } public override void onActionExecuting(ActionExecutingContext filterContext) { if ((filterContext.HttpContext.Request.ContentType ?? string.Empty).Contains("application/json")) { object o = new DataContractJsonSerializer(RootType).ReadObject(filterContext.HttpContext.Request.InputStream); filterContext.ActionParameters[Param] = o; } }}然后可以将其应用于您的控制器方法,如下所示:
[ObjectFilter(Param = "postdata", RootType = typeof(ObjectToSerializeTo))] public JsonResult ControllerMethod(ObjectToSerializeTo postdata) { ... }因此,基本上,如果帖子的内容类型是“ application / json”,则它将生效,并将值映射到您指定的类型的对象。



