英文:
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='https://fonts.googleapis.com/css?family=Nunito\u002BSans' rel='stylesheet' /\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<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='https://fonts.googleapis.com/css?family=Nunito\u002BSans' rel='stylesheet' /\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<JsonOptions>(options =>
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论