【问题标题】:HttpResponseBase replacement in ASP.NET Core MVCASP.NET Core MVC 中的 HttpResponseBase 替换
【发布时间】:2022-02-17 09:44:48
【问题描述】:

我有一个如下所示的 CustomJsonResult 类,它是用 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));
        }
    }
}
}

我将其转换为 ASP.NET Core MVC。为此,我将 HttpResponseBase 更改为 HttpResponse 并添加了此命名空间 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));
        }
    }
}
}  

我在控制器 json 方法中使用这个类:

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

但转换后,我收到类似的错误

命名空间“System.Web”中不存在类型或命名空间名称“Script”(您是否缺少程序集引用?)

当前上下文中不存在名称“数据”

“HttpResponse”不包含“Write”的定义,并且找不到接受“HttpResponse”类型的第一个参数的可访问扩展方法“Write”(您是否缺少 using 指令或程序集引用?)

当前上下文中不存在名称“ContentEncoding”

'CustomJsonResult.ExecuteResult(ControllerContext)': 找不到合适的方法来覆盖

【问题讨论】:

  • 您确定您需要此代码吗?自 Web API 1.0 起,日期使用 ISO8601 格式进行序列化。这是所有 .NET Core 应用程序的默认格式,包括 ASP.NET Core MVC

标签: c# asp.net-mvc asp.net-core-mvc


【解决方案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;
        }
    }

在控制器中:

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

asp.net core 的 Jsonresult 类中没有 MaxJsonLength 属性。 并且“数据”属性被替换为值属性

【讨论】:

  • 我试过上面的代码。现在显示没有错误。但是在控制器方法中,可以给出什么来代替Data = responseMaxJsonLength = 86753090。目前它显示此 CustomJsonResult 类不包含 DataMaxJsonLength 的定义。
  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 我的答案已更新,请检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-02
  • 2016-07-04
  • 1970-01-01
  • 2018-06-05
  • 2016-09-09
  • 2018-02-01
相关资源
最近更新 更多