diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Host/ModelDirective.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Host/ModelDirective.cs index 9e0a2f7184..416c809eb0 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Host/ModelDirective.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Host/ModelDirective.cs @@ -42,7 +42,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host var tokens = directive.Tokens.ToArray(); if (tokens.Length >= 1) { - document.Parent = directive; return tokens[0].Content; } } @@ -62,7 +61,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host public RazorEngine Engine { get; set; } // Runs after the @inherits directive - public int Order => RazorIRPass.DefaultDirectiveClassifierOrder + 5; + public int Order => RazorIRPass.DirectiveClassifierOrder + 5; public DocumentIRNode Execute(RazorCodeDocument codeDocument, DocumentIRNode irDocument) { @@ -95,15 +94,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host public IList ModelDirectives { get; } = new List(); - public override void VisitDocument(DocumentIRNode node) - { - if (node.Parent != null) - { - ModelDirectives.Add((DirectiveIRNode)node.Parent); - } - base.VisitDocument(node); - } - public override void VisitClass(ClassDeclarationIRNode node) { if (Class == null) diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/InjectDirectiveTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/InjectDirectiveTest.cs index 89fa0b9492..3053fb767b 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/InjectDirectiveTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/InjectDirectiveTest.cs @@ -197,6 +197,19 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host // Notice we're not registering the InjectDirective.Pass here so we can run it on demand. b.AddDirective(InjectDirective.Directive); b.AddDirective(ModelDirective.Directive); + + // HACK - Remove the DefaultDirectiveIRPass so it can't remove our directives. + IRazorEngineFeature badFeature = null; + foreach (var feature in b.Features) + { + if (string.Equals("DefaultDirectiveIRPass", feature.GetType().Name, StringComparison.Ordinal)) + { + badFeature = feature; + break; + } + } + + b.Features.Remove(badFeature); }); } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelDirectiveTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelDirectiveTest.cs index 35b8d4d769..c547641050 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelDirectiveTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelDirectiveTest.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.IO; using System.Text; using Microsoft.AspNetCore.Razor.Evolution; @@ -179,6 +180,19 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host { // Notice we're not registering the ModelDirective.Pass here so we can run it on demand. b.AddDirective(ModelDirective.Directive); + + // HACK - Remove the DefaultDirectiveIRPass so it can't remove our directives. + IRazorEngineFeature badFeature = null; + foreach (var feature in b.Features) + { + if (string.Equals("DefaultDirectiveIRPass", feature.GetType().Name, StringComparison.Ordinal)) + { + badFeature = feature; + break; + } + } + + b.Features.Remove(badFeature); }); }