diff --git a/src/Microsoft.AspNetCore.Blazor.Razor.Extensions/BlazorRuntimeNodeWriter.cs b/src/Microsoft.AspNetCore.Blazor.Razor.Extensions/BlazorRuntimeNodeWriter.cs index f428340932..a544c10bed 100644 --- a/src/Microsoft.AspNetCore.Blazor.Razor.Extensions/BlazorRuntimeNodeWriter.cs +++ b/src/Microsoft.AspNetCore.Blazor.Razor.Extensions/BlazorRuntimeNodeWriter.cs @@ -594,7 +594,21 @@ namespace Microsoft.AspNetCore.Blazor.Razor writer.Write(" + "); } + // If it's a C# expression, we have to wrap it in parens, otherwise + // things like ternary expressions don't compose with concatenation + var isCSharp = concatenatedValue is IntermediateToken intermediateToken + && intermediateToken.Kind == TokenKind.CSharp; + if (isCSharp) + { + writer.Write("("); + } + WriteAttributeValue(writer, concatenatedValue); + + if (isCSharp) + { + writer.Write(")"); + } } break; } diff --git a/test/Microsoft.AspNetCore.Blazor.Build.Test/RenderingRazorIntegrationTest.cs b/test/Microsoft.AspNetCore.Blazor.Build.Test/RenderingRazorIntegrationTest.cs index 03f30cae4b..45b9dc40b3 100644 --- a/test/Microsoft.AspNetCore.Blazor.Build.Test/RenderingRazorIntegrationTest.cs +++ b/test/Microsoft.AspNetCore.Blazor.Build.Test/RenderingRazorIntegrationTest.cs @@ -180,6 +180,20 @@ namespace Microsoft.AspNetCore.Blazor.Build.Test frame => AssertFrame.Attribute(frame, "attr", "Hello, WORLD with number 246!", 1)); } + [Fact] + public void SupportsAttributesWithInterpolatedTernaryExpressionValues() + { + // Arrange/Act + var component = CompileToComponent( + "@{ var myValue = \"world\"; }" + + ""); + + // Assert + Assert.Collection(GetRenderTree(component), + frame => AssertFrame.Element(frame, "elem", 2, 0), + frame => AssertFrame.Attribute(frame, "attr", "Hello, world!", 1)); + } + [Fact] public void SupportsHyphenedAttributesWithCSharpExpressionValues() {