This code was working just fine before i moved to 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); ;
}
here is the JSonFilterAttribute code
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;
}
}
}
Now The JsonFilter doesn't get the object anymore. It always return null ?
Is there something i have to do in MVC3 ?
You no longer need this attribute as ASP.NET MVC 3 has this functionality built-in.
精彩评论