2011-03-07 69 views
1

此代碼是工作得很好。當我MVC3 ...MVC3與JsonFilterAttribute不工作了

 [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?

回答

2

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

+0

那麼我試圖評論屬性,但我仍然得到一個空對象的所有時間。我是否需要在其他地方加入別的東西? – mate0 2011-03-07 20:38:06

+0

鏈接是404.是否有更新的參考?也許這一個:http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx – 2013-04-10 18:23:03