2011-07-08 25 views
1

如何擴展JsonResult?假設我想製作一個JsonTransactionResult,因爲我想強制執行所有的交易以返回一個陳化的TransactionResult對象。 TransactionResult對象包含錯誤消息和內容的數據。我是通過繼承還是包裝JsonResult擴展JsonResult

回答

3

我只是從JsonResult繼承並返回TransactionResult類的實例。

我有類似的東西,雖然我從ActionResult繼承並使用JSON.NET,因爲我使用內置的JsonResult對DateTime有一些序列化問題。

/// <summary> 
/// A Newtonsoft.Json based JsonResult for ASP.NET MVC 
/// </summary> 
public class JsonNetResult : ActionResult 
{ 
    /// <summary> 
    /// Initializes a new instance of the <see cref="JsonNetResult"/> class. 
    /// </summary> 
    public JsonNetResult() 
    { 
     this.SerializerSettings = new JsonSerializerSettings(); 
    } 

    /// <summary> 
    /// Gets or sets the content encoding. 
    /// </summary> 
    /// <value>The content encoding.</value> 
    public Encoding ContentEncoding { get; set; } 

    /// <summary> 
    /// Gets or sets the type of the content. 
    /// </summary> 
    /// <value>The type of the content.</value> 
    public string ContentType { get; set; } 

    /// <summary> 
    /// Gets or sets the data. 
    /// </summary> 
    /// <value>The data object.</value> 
    public object Data { get; set; } 

    /// <summary> 
    /// Gets or sets the serializer settings. 
    /// </summary> 
    /// <value>The serializer settings.</value> 
    public JsonSerializerSettings SerializerSettings { get; set; } 

    /// <summary> 
    /// Gets or sets the formatting. 
    /// </summary> 
    /// <value>The formatting.</value> 
    public Formatting Formatting { get; set; } 

    /// <summary> 
    /// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult"/> class. 
    /// </summary> 
    /// <param name="context">The context in which the result is executed. The context information includes the controller, HTTP content, request context, and route data.</param> 
    public override void ExecuteResult(ControllerContext context) 
    { 
     if (context == null) 
     { 
      throw new ArgumentNullException("context"); 
     } 

     HttpResponseBase response = context.HttpContext.Response; 

     response.ContentType = !String.IsNullOrWhiteSpace(this.ContentType) ? this.ContentType : "application/json"; 

     if (this.ContentEncoding != null) 
     { 
      response.ContentEncoding = this.ContentEncoding; 
     } 

     if (this.Data != null) 
     { 
      JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = this.Formatting }; 

      JsonSerializer serializer = JsonSerializer.Create(this.SerializerSettings); 
      serializer.Serialize(writer, this.Data); 

      writer.Flush(); 
     } 
    } 
} 

然後我從該類繼承,包裝一個成功的屬性與結果:

/// <summary> 
/// Derives from <see cref="JsonNetResult"/>. This action result can be used to wrap an AJAX callback result with a status code and a description, along with the actual data. 
/// </summary> 
public class CallbackJsonResult : JsonNetResult 
{ 
    /// <summary> 
    /// Initializes a new instance of the <see cref="CallbackJsonResult"/> class. 
    /// </summary> 
    /// <param name="statusCode">The status code.</param> 
    public CallbackJsonResult(HttpStatusCode statusCode) 
    { 
     this.Initialize(statusCode, null, null); 
    } 

    /// <summary> 
    /// Initializes a new instance of the <see cref="CallbackJsonResult"/> class. 
    /// </summary> 
    /// <param name="statusCode">The status code.</param> 
    /// <param name="description">The description.</param> 
    public CallbackJsonResult(HttpStatusCode statusCode, string description) 
    { 
     this.Initialize(statusCode, description, null); 
    } 

    /// <summary> 
    /// Initializes a new instance of the <see cref="CallbackJsonResult"/> class. 
    /// </summary> 
    /// <param name="statusCode">The status code.</param> 
    /// <param name="data">The callback result data.</param> 
    public CallbackJsonResult(HttpStatusCode statusCode, object data) 
    { 
     this.Initialize(statusCode, null, data); 
    } 

    /// <summary> 
    /// Initializes a new instance of the <see cref="CallbackJsonResult"/> class. 
    /// </summary> 
    /// <param name="statusCode">The status code.</param> 
    /// <param name="description">The description.</param> 
    /// <param name="data">The callback result data.</param> 
    public CallbackJsonResult(HttpStatusCode statusCode, string description, object data) 
    { 
     this.Initialize(statusCode, description, data); 
    } 

    /// <summary> 
    /// Initializes this instance. 
    /// </summary> 
    /// <param name="statusCode">The status code.</param> 
    /// <param name="description">The description.</param> 
    /// <param name="data">The callback result data.</param> 
    private void Initialize(HttpStatusCode statusCode, string description, object data) 
    { 
     Data = new { Success = statusCode == HttpStatusCode.OK, Status = (int)statusCode, Description = description, Data = data }; 
    } 
} 
+0

哇。奇蹟般有效!非常感謝! – Jonn

+0

在James Newton-King的網站上找到的用法示例,他提出了他的JsonNetResult ... [link] http://james.newtonking.com/archive/2008/10/16/asp-net-mvc-and-json -net.aspx – sirthomas