Esempio n. 1
0
        public static void Register(RazorProjectEngineBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            InjectDirective.Register(builder);
            ModelDirective.Register(builder);
            NamespaceDirective.Register(builder);
            PageDirective.Register(builder);

            FunctionsDirective.Register(builder);
            InheritsDirective.Register(builder);
            SectionDirective.Register(builder);

            builder.Features.Add(new DefaultTagHelperDescriptorProvider());
            builder.Features.Add(new ViewComponentTagHelperDescriptorProvider());

            builder.AddTargetExtension(new ViewComponentTagHelperTargetExtension());
            builder.AddTargetExtension(new TemplateTargetExtension()
            {
                TemplateTypeName = "global::Microsoft.AspNetCore.Mvc.Razor.HelperResult",
            });

            builder.Features.Add(new ModelExpressionPass());
            builder.Features.Add(new PagesPropertyInjectionPass());
            builder.Features.Add(new ViewComponentTagHelperPass());
            builder.Features.Add(new RazorPageDocumentClassifierPass());
            builder.Features.Add(new MvcViewDocumentClassifierPass());
            builder.Features.Add(new AssemblyAttributeInjectionPass());
            builder.Features.Add(new InstrumentationPass());

            builder.SetImportFeature(new MvcImportProjectFeature());
        }
Esempio n. 2
0
        protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
        {
            if (documentNode.DocumentKind != RazorPageDocumentClassifierPass.RazorPageDocumentKind)
            {
                return;
            }

            var modelType = ModelDirective.GetModelType(documentNode);
            var visitor   = new Visitor();

            visitor.Visit(documentNode);

            var @class = visitor.Class;

            var viewDataType = $"global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<{modelType}>";
            var vddProperty  = new CSharpCodeIntermediateNode();

            vddProperty.Children.Add(new IntermediateToken()
            {
                Kind    = TokenKind.CSharp,
                Content = $"public {viewDataType} ViewData => ({viewDataType})PageContext?.ViewData;",
            });
            @class.Children.Add(vddProperty);

            var modelProperty = new CSharpCodeIntermediateNode();

            modelProperty.Children.Add(new IntermediateToken()
            {
                Kind    = TokenKind.CSharp,
                Content = $"public {modelType} Model => ViewData.Model;",
            });
            @class.Children.Add(modelProperty);
        }
Esempio n. 3
0
        public void ModelDirective_GetModelType_DefaultsToDynamic()
        {
            // Arrange
            var codeDocument = CreateDocument(@" ");

            var engine = CreateRuntimeEngine();

            var irDocument = CreateIRDocument(engine, codeDocument);

            // Act
            var result = ModelDirective.GetModelType(irDocument);

            // Assert
            Assert.Equal("dynamic", result);
        }
Esempio n. 4
0
        public void ModelDirective_GetModelType_GetsTypeFromFirstWellFormedDirective()
        {
            // Arrange
            var codeDocument = CreateDocument(@"
@model Type1
@model Type2
@model
");

            var engine = CreateRuntimeEngine();

            var irDocument = CreateIRDocument(engine, codeDocument);

            // Act
            var result = ModelDirective.GetModelType(irDocument);

            // Assert
            Assert.Equal("Type1", result);
        }
Esempio n. 5
0
            protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
            {
                var visitor = new Visitor();

                visitor.Visit(documentNode);
                var modelType = ModelDirective.GetModelType(documentNode);

                var properties = new HashSet <string>(StringComparer.Ordinal);

                for (var i = visitor.Directives.Count - 1; i >= 0; i--)
                {
                    var directive = visitor.Directives[i];
                    var tokens    = directive.Tokens.ToArray();
                    if (tokens.Length < 2)
                    {
                        continue;
                    }

                    var typeName   = tokens[0].Content;
                    var memberName = tokens[1].Content;

                    if (!properties.Add(memberName))
                    {
                        continue;
                    }

                    typeName = typeName.Replace("<TModel>", "<" + modelType + ">");

                    var injectNode = new InjectIntermediateNode()
                    {
                        TypeName   = typeName,
                        MemberName = memberName,
                    };

                    visitor.Class.Children.Add(injectNode);
                }
            }