0

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

1
  • Are you sure you need this code at all? Dates are serialized using the ISO8601 format since Web API 1.0. That's the default format for all .NET Core applications, including ASP.NET Core MVC Commented Feb 11, 2022 at 9:17

1 Answer 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

Sign up to request clarification or add additional context in comments.

3 Comments

I tried above code. It shows no error now. But in controller method, what can be given in place of Data = response and MaxJsonLength = 86753090. Currently it shows this CustomJsonResult class does not contain definition for Data and MaxJsonLength.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
I've updated my answer,please check it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.