'HttpResponseBase replacement in ASP.NET Core MVC

I have a CustomJsonResult class shown below, which is written in ASP.NET MVC:

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;


namespace Web.Authentication
{
public class CustomJsonResult : JsonResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        HttpResponseBase response = context.HttpContext.Response;

        if (!String.IsNullOrEmpty(ContentType))
        {
            response.ContentType = ContentType;
        }
        else
        {
            response.ContentType = "application/json";
        }
        if (ContentEncoding != null)
        {
            response.ContentEncoding = ContentEncoding;
        }
        if (Data != null)
        {                
            var isoConvert = new IsoDateTimeConverter();
            isoConvert.DateTimeFormat = _dateFormat;
            response.Write(JsonConvert.SerializeObject(Data, isoConvert));
        }
    }
}
}

I converted this to ASP.NET Core MVC. For that I changed the HttpResponseBase to HttpResponse and added this namespace using Microsoft.AspNetCore.Http;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;


namespace Web.Authentication
{
public class CustomJsonResult : JsonResult
{        
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        HttpResponse response = context.HttpContext.Response;

        if (!String.IsNullOrEmpty(ContentType))
        {
            response.ContentType = ContentType;
        }
        else
        {
            response.ContentType = "application/json";
        }
        if (ContentEncoding != null)
        {
            response.ContentEncoding = ContentEncoding;
        }
        if (Data != null)
        {                
            var isoConvert = new IsoDateTimeConverter();
            isoConvert.DateTimeFormat = _dateFormat;
            response.Write(JsonConvert.SerializeObject(Data, isoConvert));
        }
    }
}
}  

I'm using this class in controller json method:

[HttpPost]
    [ValidateAntiForgeryToken]
    public JsonResult GetDataRefreshDate()
    {            
        var response = ...
        return new CustomJsonResult()
        {
            Data = response,
            MaxJsonLength = 86753090
        };
    }

But After converting, I'm getting errors like

The type or namespace name 'Script' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

The name 'Data' does not exist in the current context

'HttpResponse' does not contain a definition for 'Write' and no accessible extension method 'Write' accepting a first argument of type 'HttpResponse' could be found (are you missing a using directive or an assembly reference?)

The name 'ContentEncoding' does not exist in the current context

'CustomJsonResult.ExecuteResult(ControllerContext)': no suitable method found to override



Solution 1:[1]

public class CustomJsonResult1 : JsonResult
    {

       
        public CustomJsonResult1(object value) : base(value)
        {
           
        }
        public int MaxJsonLength { get; set; }

        public override Task ExecuteResultAsync(ActionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            HttpResponse response = context.HttpContext.Response;
            if (!String.IsNullOrEmpty(ContentType))
            {
                response.ContentType = ContentType;
            }
            else
            {
                response.ContentType = "application/json";
            }
            var mediaType = new MediaTypeHeaderValue(ContentType);
            if (mediaType.Encoding != null)
            {
                response.ContentType = response.ContentType;
            }

            if (Value != null)
            {
                var isoConvert = new IsoDateTimeConverter();
                isoConvert.DateTimeFormat = null;

                response.WriteAsync(JsonConvert.SerializeObject(Value, isoConvert));
            }
            return Task.CompletedTask;
        }
    }

In controller?

return new CustomJsonResult1(new { result = false, msg = "???" });

There is no MaxJsonLength property in Jsonresult class in asp.net core. and the "Data"property is replaced with value propert

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1