[英]Unicode Normalization Form C in ASP.NET Core Razor view
I am doing a W3C validation on an ASP.NET Core 2.2 Razor View but W3C gives me warning: 我在ASP.NET Core 2.2 Razor View上进行W3C验证 ,但是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.
警告:来自名称空间http://www.w3.org/1999/xhtml的元素img上的属性alt的值不在Unicode规范化形式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.
警告:来自名称空间http://www.w3.org/1999/xhtml的元素img上的属性标题的值不在Unicode规范化形式C中。
My data is stored in a MSSQL database as nvarchar
and everything else is set to UTF-8. 我的数据以
nvarchar
形式存储在MSSQL数据库中,其他所有数据都设置为UTF-8。
Controller 调节器
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 view 剃刀视图
@model FileViewModel
<img src="@Model.Source" alt="@Model.Title" title="@Model.Title" />
Output 产量
<!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="Forår" title="Forår" />
</body>
</html>
It seems W3C does not like the string "Forår"
but needs "Forår"
instead. 似乎W3C不喜欢字符串
"Forår"
而是需要"Forår"
。
How can I force ASP.NET Core Razor views to produce proper formatting? 如何强制ASP.NET Core Razor视图产生正确的格式?
There are two ways to represent the character å
: 有两种表示字符
å
:
Using the Unicode codepoint U+00E5 ( latin small letter A with ring above ). 使用Unicode代码点U + 00E5(带小环的拉丁文小写字母A )。
Using the combination of the two code points U+0061 ( latin small letter A ) and U+030A ( combining ring above ). 使用两个代码点U + 0061( 拉丁小写字母A )和U + 030A( 上面的组合环 )的组合 。
Visually, they cannot be distinguished. 在视觉上,它们无法区分。 And it is not related to HTML, which can represent both versions - either directly (using UTF-8, UTF-16) or using an escaped version (like
̊
). 而且它与HTML无关,后者可以表示两个版本-直接(使用UTF-8,UTF-16) 或使用转义版本(例如
̊
)。
In your case, the second way to represent å
is used. 在您的情况下,将使用第二种表示
å
方式。
In order to manage this ambiguity, Unicode has four normalization forms : C , D , KC and KD . 为了处理这种歧义,Unicode具有四种规范化形式 : C , D , KC和KD 。
Unicode normalization is also supported by C#, more specifically by String.Normalize . C#还支持Unicode规范化,更具体地说, String.Normalize也支持。
So in order get rid of the warning, normalize the 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);
}
In the long run, it's probably better to normalize all strings before storing them into a database. 从长远来看,最好将所有字符串标准化后再将它们存储到数据库中。 If the same text can be represented with different codepoint sequences, it will also cause troubles in database queries.
如果相同的文本可以用不同的代码点序列表示,则也会在数据库查询中引起麻烦。 And most database are unable to normalize strings.
而且大多数数据库无法规范化字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.