带有JsonFilterAttribute的MVC3不再起作用

时间:2011-03-07 20:20:26

标签: json asp.net-mvc-3 filter

在我搬到MVC3之前,这段代码工作得很好......

        [HttpPost]
    [ActionName("GetCommentListForServiceCall")]
    [UrlRoute(Path = "mobile/servicecalls/{id}/comments", Order=1)]
    [UrlRouteParameterConstraint(Name = "id", Regex = @"\d+")]
    [OutputCache(CacheProfile = "MobileCacheProfile")]
    [JsonFilter(JsonDataType = typeof(ServiceCallCommentNewDTO), Param = "comment")]
    public JsonNetResult CreateCommentForServiceCall(int id, ServiceCallCommentNewDTO comment)
    {
        ServiceCallComment entity = coreSvc.SaveServiceCallComment(id, 0, comment.CategoryId, comment.Comment);
        SetResponseCode(HttpStatusCode.Created);
        SetContentLocation(string.Format("mobile/servicecalls/{0}/comments/{1}", id.ToString(), entity.Id.ToString()));
        return new JsonNetResult(entity); ;
    }

这里是JSonFilterAttribute代码

    public class JsonFilterAttribute : ActionFilterAttribute
{
    public string Param { get; set; }
    public Type JsonDataType { get; set; }
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))
        {
            string inputContent;
            using (var sr = new StreamReader(filterContext.HttpContext.Request.InputStream))
            {
                inputContent = sr.ReadToEnd();
            }
            var result = JsonConvert.DeserializeObject(inputContent, JsonDataType, new JsonSerializerSettings{TypeNameHandling = TypeNameHandling.All});
            filterContext.ActionParameters[Param] = result;
        }
    }
}

现在JsonFilter不再获取对象了。它总是返回null?

我在MVC3中有什么需要做的吗?

1 个答案:

答案 0 :(得分:2)

您不再需要此属性,因为ASP.NET MVC 3具有this functionality built-in