Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6121887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:50:06+00:00 2026-05-23T15:50:06+00:00

How do you extend JsonResult ? Say I wanted to make a JsonTransactionResult because

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:50:07+00:00Added an answer on May 23, 2026 at 3:50 pm

    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 };
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to extend an existing application I made to make it set mixer
I want to extend a WPF application with database functionality. Which database engine would
I want to extend the basic ControlCollection in VB.NET so I can just add
To extend the User object with custom fields, the Django docs recommend using UserProfiles
We want to extend the bug work item template with a new field called
i want to extend an xsd:complexType with another xsd:complexType. I want to extend both
I want to extend a class as shown in the below example. While extending,
Is it possible to extend the console object? I tried something like: Console.prototype.log =
function extend(o, p) { for(prop in p) { o[prop] = p[prop]; } return o;
I want to extend DataGrid to add some controls on the very top of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.