如何在ASP.NET Core中关闭JsonResults的字符转义。

huangapple go评论72阅读模式
英文:

How to turn off character escaping for JsonResults in ASP.NET Core

问题

I've recently converted an application from using .NET Framework to .NET Core and in the process some of my endpoints have stopped producing the expected result due to escaping certain characters when returning them.

我的应用程序最近从.NET Framework转换为.NET Core,在这个过程中,一些我的端点因为返回时转义了某些字符而停止产生预期的结果。

My controllers broadly look like this:

我的控制器大致如下:

class MyController
{
    [HttpGet]
    [Authenticated]
    public async Task<ActionResult> GetMyObject(string id)
    {
        var myObj = // Controller specific code to look up the object
        return new JsonResult(myObj);
    }
}

This was previously returning a result like this:

这之前返回的结果如下:

{"HtmlTemplate":"<!DOCTYPE html><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><head><link href='https://fonts.googleapis.com/css?family=Nunito+Sans' rel='stylesheet' /><style>

Whereas now it looks like

而现在看起来像这样

{"htmlTemplate":"\u003C!DOCTYPE html\u003E\u003Chtml lang=\u0022en\u0022 xmlns=\u0022http://www.w3.org/1999/xhtml\u0022\u003E\u003Chead\u003E\u003Clink href=&#39;https://fonts.googleapis.com/css?family=Nunito\u002BSans&#39; rel=&#39;stylesheet&#39; /\u003E\u003Cstyle\u003E

I have less control over modifying the caller of the endpoint, so I'd like to address that in this application. I'm familiar with how to change the formatting on a serializer, like here, but I don't know how to set the parameters here since it's implicit.

我无法完全控制对端点的调用者进行修改,因此我想在此应用程序中解决这个问题。我知道如何更改序列化程序的格式,例如这里,但我不知道如何在这里设置参数,因为它是隐式的。

英文:

I've recently converted an application from using .NET Framework to .NET Core and in the process some of my endpoints have stopped producing the expected result due to escaping certain characters when returning them.

My controllers broadly look like this:

class MyController
{
    [HttpGet]
    [Authenticated]
    public async Task&lt;ActionResult&gt; GetMyObject(string id)
    {
        var myObj = // Controller specific code to look up the object
        return new JsonResult(myObj);
    }
}

This was previously returning a result like this:

{&quot;HtmlTemplate&quot;:&quot;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
 &lt;link href=&#39;https://fonts.googleapis.com/css?family=Nunito+Sans&#39; rel=&#39;stylesheet&#39; /&gt;
 &lt;style&gt;

Whereas now it looks like

{&quot;htmlTemplate&quot;:&quot;
\u003C!DOCTYPE html\u003E
\u003Chtml lang=\u0022en\u0022 xmlns=\u0022http://www.w3.org/1999/xhtml\u0022\u003E
\u003Chead\u003E
 \u003Clink href=&#39;https://fonts.googleapis.com/css?family=Nunito\u002BSans&#39; rel=&#39;stylesheet&#39; /\u003E
 
 \u003Cstyle\u003E

I have less control over modifying the caller of the endpoint, so I'd like to address that in this application. I'm familiar with how to change the formatting on a serializer, like here, but I don't know how to set the parameters here since it's implicit.

答案1

得分: 1

你需要配置JsonOptions

using Microsoft.AspNetCore.Mvc;

// ...
builder.Services.Configure<JsonOptions>(options =>
    options.JsonSerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping);

请注意,有两个JsonOptions,一个来自Microsoft.AspNetCore.Mvc命名空间,另一个来自Microsoft.AspNetCore.Http.Json命名空间,对于控制器,您需要前者。

另一种方法是使用所需的设置手动进行序列化,然后返回ContentResult(根据需要在每个端点上处理此操作)。

英文:

You need to configure JsonOptions:

using Microsoft.AspNetCore.Mvc;

// ...
builder.Services.Configure&lt;JsonOptions&gt;(options =&gt;
    options.JsonSerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping);

Note that there are 2 JsonOptions, one from Microsoft.AspNetCore.Mvc namespace and one from Microsoft.AspNetCore.Http.Json namespace, for controllers you need the former one.

Another approach can be to serialize "manually" with needed settings and return ContentResult (to handle this on per endpoint basis if needed).

huangapple
  • 本文由 发表于 2023年4月19日 23:09:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定