扩展 JsonResult

发布于 2024-11-18 22:42:46 字数 223 浏览 4 评论 0原文

如何扩展JsonResult?假设我想要创建一个 JsonTransactionResult,因为我想强制所有事务返回一个 json 化的 TransactionResult 对象。 TransactionResult 对象包含错误消息等数据。我是通过继承还是包装 JsonResult 来做到这一点?

How do you extend JsonResult? Say I wanted to make a JsonTransactionResult because I want to enforce all of my transactions to return a jsonified TransactionResult object. The TransactionResult object contains data for error messages and stuff. Do I do this via inheritance or wrapping JsonResult?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2024-11-25 22:42:46

我只需继承 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 };
    }
}

I'd simply inherit from JsonResult and return an instance of the TransactionResult class.

I have something similar, though I inherit from ActionResult and use JSON.NET, since I had some serialization issues with DateTime using the built in JsonResult.

/// <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();
        }
    }
}

And then I inherit from that class, to wrap a success property with the result:

/// <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 };
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文