dotnet / dotnet/aspnetcore

HtmlContentBuilderExtensions.AppendFormat Method

Open
#52,471 0 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

I am not sure if this is really a bug or is by design, but it threw me for a loop because other parts of the documentation state that interpolated strings can be used instead of an array of arguments. However, the [HtmlContentBuilderExtensions.AppendFormat](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.html.htmlcontentbuilderextensions.appendformat?view=aspnetcore-7.0) method **does not work with interpolated strings**.

For example, consider the following code:

```
String fld_name = "fld1";
String fld_val = "alert('hello')";

HtmlContentBuilder html_builder = new HtmlContentBuilder();

// AppendFormat:
// Appends the specified format to the existing content AFTER REPLACING EACH FORMAT ITEM
// WITH THE HTML ENCODED STRING REPRESENTATION of the corresponding item in the args array ...

// Test with interpolated string -- does not work ! ...

html_builder.AppendFormat( $@"" );
html_builder.AppendLine();

// Test with format string and ags array ...

html_builder.AppendFormat( @"", fld_name, fld_val );
html_builder.AppendLine();

// Write the content ...

using ( var writer = new System.IO.StringWriter() )
{
html_builder.WriteTo( writer, HtmlEncoder.Default );
Console.WriteLine( writer.ToString() );
}
```

The output produced is:

```

```

### Expected Behavior

I was expecting the same result for both, i.e.:

```

```

If this behavior is by design can the documentation PLEASE be updated to indicate a **Remarks** section that indicates something like:

**Please Note:** Interpolated strings are NOT supported by this method.

### Steps To Reproduce

Code supplied above.

### Exceptions (if any)

_No response_

### .NET Version

8.0.100

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.