diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorRuntimeCSharpLoweringPhase.cs b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorRuntimeCSharpLoweringPhase.cs index 8c1f5a2150..f54c297011 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorRuntimeCSharpLoweringPhase.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorRuntimeCSharpLoweringPhase.cs @@ -12,18 +12,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution { internal class DefaultRazorRuntimeCSharpLoweringPhase : RazorCSharpLoweringPhaseBase { - private static string _tagHelperId; - - internal static string GenerateUniqueTagHelperId { - get - { - return _tagHelperId ?? Guid.NewGuid().ToString("N"); - } - set - { - _tagHelperId = value; - } - } + internal static readonly object SuppressUniqueIds = "SuppressUniqueIds"; protected override void ExecuteCore(RazorCodeDocument codeDocument) { @@ -39,6 +28,13 @@ namespace Microsoft.AspNetCore.Razor.Evolution SourceDocument = codeDocument.Source, Options = syntaxTree.Options, }; + + var idValue = codeDocument.Items[SuppressUniqueIds]; + if (idValue != null) + { + renderingContext.IdGenerator = () => idValue.ToString(); + } + var visitor = new CSharpRenderer(renderingContext); visitor.VisitDocument(irDocument); var csharpDocument = new RazorCSharpDocument() @@ -300,7 +296,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution .Write(".") .Write(node.TagMode.ToString()) .WriteParameterSeparator() - .WriteStringLiteral(GenerateUniqueTagHelperId) + .WriteStringLiteral(Context.IdGenerator()) .WriteParameterSeparator(); // We remove and redirect writers so TagHelper authors can retrieve content. diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/RazorCSharpLoweringPhaseBase.cs b/src/Microsoft.AspNetCore.Razor.Evolution/RazorCSharpLoweringPhaseBase.cs index 9889f36f3f..b6f6eacdae 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/RazorCSharpLoweringPhaseBase.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/RazorCSharpLoweringPhaseBase.cs @@ -189,6 +189,8 @@ namespace Microsoft.AspNetCore.Razor.Evolution public ICollection Directives { get; set; } + public Func IdGenerator { get; set; } = () => Guid.NewGuid().ToString("N"); + public List LineMappings { get; } = new List(); public CSharpCodeWriter Writer { get; set; } diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/CodeGenerationIntegrationTest.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/CodeGenerationIntegrationTest.cs index cbdc658754..76282a8269 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/CodeGenerationIntegrationTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/CodeGenerationIntegrationTest.cs @@ -1408,7 +1408,11 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests throw new XunitException($"The resource {sourceFilename} was not found."); } - return RazorCodeDocument.Create(TestRazorSourceDocument.CreateResource(sourceFilename)); + var codeDocument = RazorCodeDocument.Create(TestRazorSourceDocument.CreateResource(sourceFilename)); + + // This will ensure that we're not putting any randomly generated data in a baseline. + codeDocument.Items[DefaultRazorRuntimeCSharpLoweringPhase.SuppressUniqueIds] = "test"; + return codeDocument; } private void RunRuntimeTagHelpersTest(IEnumerable descriptors) diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/IntegrationTestBase.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/IntegrationTestBase.cs index 4165a7a54c..405643496e 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/IntegrationTestBase.cs +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/IntegrationTests/IntegrationTestBase.cs @@ -88,10 +88,11 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests throw new XunitException($"The resource {sourceFilename} was not found."); } - // This will ensure that we're not putting any randomly generated data in a baseline. - DefaultRazorRuntimeCSharpLoweringPhase.GenerateUniqueTagHelperId = "test"; + var codeDocument = RazorCodeDocument.Create(TestRazorSourceDocument.CreateResource(sourceFilename)); - return RazorCodeDocument.Create(TestRazorSourceDocument.CreateResource(sourceFilename)); + // This will ensure that we're not putting any randomly generated data in a baseline. + codeDocument.Items[DefaultRazorRuntimeCSharpLoweringPhase.SuppressUniqueIds] = "test"; + return codeDocument; } protected void AssertIRMatchesBaseline(DocumentIRNode document)