ASP.NET Core Razor 视图中的 Unicode 规范化形式 C

Unicode Normalization Form C in ASP.NET Core Razor view

我正在 ASP.NET Core 2.2 Razor View 上做 W3C validation 但 W3C 给我警告:

Warning: The value of attribute alt on element img from namespace http://www.w3.org/1999/xhtml is not in Unicode Normalization Form C.

Warning: The value of attribute title on element img from namespace http://www.w3.org/1999/xhtml is not in Unicode Normalization Form C.

我的数据以 nvarchar 的形式存储在 MSSQL 数据库中,其他所有内容都设置为 UTF-8。

控制器

    using Dapper;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class FileViewModel
    {
        public int FileId { get; set; }
        public string Title { get; set; }
        public string Source { get; set; }
    }

    private async Task<FileViewModel> LoadFileAsync(int id)
    {
        using (SqlConnection conn = new SqlConnection("Conn-string-here"))
        {
            const string sql = "SELECT * FROM dbo.Files WHERE FileId=@Id";

            var data = await conn.QueryAsync<FileViewModel>(sql, new { id }).ConfigureAwait(false);
            return data.FirstOrDefault();
        }
    }

    [Route("~/file/{id}")]
    public async Task<IActionResult> File(int id)
    {
        FileViewModel m = await LoadFileAsync(id).ConfigureAwait(false);
        Return View(m);
    }

Razor 视图

@model FileViewModel

<img src="@Model.Source" alt="@Model.Title" title="@Model.Title" />

输出

<!DOCTYPE html>
<html lang="da">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Title</title>
    <meta name="description" content="my description" />
    <meta name="keywords" content="my keywords" />
</head>
<body>

<!-- Auto generated from database: -->
<img src="https://example.org/img.png" alt="Fora&#x30A;r" title="Fora&#x30A;r" />

</body>
</html>

W3C 似乎不喜欢字符串 "Fora&#x30A;r",而是需要 "Forår"

如何强制 ASP.NET Core Razor 视图生成正确的格式?

字符有两种表示方式å:

  1. 使用 Unicode 代码点 U+00E5 (latin small letter A with ring above)。

  2. 使用U+0061(latin small letter A) and U+030A (combining ring above)两个码位的组合。

视觉上无法区分它们。而且它与 HTML 无关,后者可以表示两个版本 - 直接(使用 UTF-8、UTF-16) 使用转义版本(如 &#x30A;).

在您的例子中,使用了第二种表示方式 å

为了管理这种歧义,Unicode 有四个 normalization formsCD KCKD.

C# 也支持 Unicode 规范化,更具体地说 String.Normalize

所以为了摆脱警告,规范化 title:

[Route("~/file/{id}")]
public async Task<IActionResult> File(int id)
{
    FileViewModel m = await LoadFileAsync(id).ConfigureAwait(false);
    m.Title = m.Title.Normalize(NormalizationForm.FormC);
    return View(m);
}

在长 运行 中,最好先规范化所有字符串,然后再将它们存储到数据库中。如果同一个文本可以用不同的codepoint序列表示,也会给数据库查询带来麻烦。而且大多数数据库都无法规范化字符串。