【发布时间】:2019-07-09 02:46:45
【问题描述】:
我在 ASP.NET Core 2.2 Razor 视图上执行W3C validation,但 W3C 给了我警告:
警告:来自命名空间的元素 img 上的属性 alt 的值 http://www.w3.org/1999/xhtml 不在 Unicode 规范化表单 C 中。
警告:来自命名空间的元素 img 上的属性 title 的值 http://www.w3.org/1999/xhtml 不在 Unicode 规范化表单 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);
}
剃刀视图
@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="Forår" title="Forår" />
</body>
</html>
W3C 似乎不喜欢字符串"Fora&#x30A;r",而是需要"Forår"。
如何强制 ASP.NET Core Razor 视图生成正确的格式?
【问题讨论】:
标签: c# asp.net-core unicode razor-pages