diff --git a/src/Html.Abstractions/src/HtmlContentBuilderExtensions.cs b/src/Html.Abstractions/src/HtmlContentBuilderExtensions.cs
index 53be2049e3d9..202893e162cf 100644
--- a/src/Html.Abstractions/src/HtmlContentBuilderExtensions.cs
+++ b/src/Html.Abstractions/src/HtmlContentBuilderExtensions.cs
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Diagnostics.CodeAnalysis;
+
namespace Microsoft.AspNetCore.Html;
///
@@ -24,7 +26,7 @@ public static class HtmlContentBuilderExtensions
/// A reference to this instance after the append operation has completed.
public static IHtmlContentBuilder AppendFormat(
this IHtmlContentBuilder builder,
- string format,
+ [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format,
params object?[] args)
{
if (builder == null)
@@ -64,7 +66,7 @@ public static IHtmlContentBuilder AppendFormat(
public static IHtmlContentBuilder AppendFormat(
this IHtmlContentBuilder builder,
IFormatProvider formatProvider,
- string format,
+ [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format,
params object?[] args)
{
if (builder == null)
diff --git a/src/Html.Abstractions/src/HtmlFormattableString.cs b/src/Html.Abstractions/src/HtmlFormattableString.cs
index 5bbc6219966a..0de6d6482b6a 100644
--- a/src/Html.Abstractions/src/HtmlFormattableString.cs
+++ b/src/Html.Abstractions/src/HtmlFormattableString.cs
@@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.Encodings.Web;
@@ -25,7 +26,7 @@ public class HtmlFormattableString : IHtmlContent
///
/// A composite format string.
/// An array that contains objects to format.
- public HtmlFormattableString(string format, params object?[] args)
+ public HtmlFormattableString([StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format, params object?[] args)
: this(formatProvider: null, format: format, args: args)
{
}
@@ -37,7 +38,10 @@ public HtmlFormattableString(string format, params object?[] args)
/// An object that provides culture-specific formatting information.
/// A composite format string.
/// An array that contains objects to format.
- public HtmlFormattableString(IFormatProvider? formatProvider, string format, params object?[] args)
+ public HtmlFormattableString(
+ IFormatProvider? formatProvider,
+ [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format,
+ params object?[] args)
{
if (format == null)
{
diff --git a/src/Razor/Razor/src/TagHelpers/TagHelperContent.cs b/src/Razor/Razor/src/TagHelpers/TagHelperContent.cs
index a8747b2bb101..b528e63edf25 100644
--- a/src/Razor/Razor/src/TagHelpers/TagHelperContent.cs
+++ b/src/Razor/Razor/src/TagHelpers/TagHelperContent.cs
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Diagnostics.CodeAnalysis;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Html;
@@ -92,7 +93,7 @@ public TagHelperContent SetHtmlContent(string encoded)
///
/// The object array to format.
/// A reference to this instance after the append operation has completed.
- public TagHelperContent AppendFormat(string format, params object[] args)
+ public TagHelperContent AppendFormat([StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format, params object[] args)
{
HtmlContentBuilderExtensions.AppendFormat(this, null, format, args);
return this;
@@ -109,7 +110,10 @@ public TagHelperContent AppendFormat(string format, params object[] args)
///
/// The object array to format.
/// A reference to this instance after the append operation has completed.
- public TagHelperContent AppendFormat(IFormatProvider provider, string format, params object[] args)
+ public TagHelperContent AppendFormat(
+ IFormatProvider provider,
+ [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format,
+ params object[] args)
{
HtmlContentBuilderExtensions.AppendFormat(this, provider, format, args);
return this;