コード例 #1
0
#pragma warning disable CS0618// Type or member is obsolete
    /// <summary>
    /// Creates a new <see cref="CompiledViewDescriptor"/>. At least one of <paramref name="attribute"/> or
    /// <paramref name="item"/> must be non-<c>null</c>.
    /// </summary>
    /// <param name="item">The <see cref="RazorCompiledItem"/>.</param>
    /// <param name="attribute">The <see cref="RazorViewAttribute"/>.</param>
    public CompiledViewDescriptor(RazorCompiledItem item, RazorViewAttribute attribute)
#pragma warning restore CS0618 // Type or member is obsolete
    {
        if (item == null && attribute == null)
        {
            // We require at least one of these to be specified.
            throw new ArgumentException(Resources.FormatCompiledViewDescriptor_NoData(nameof(item), nameof(attribute)));
        }

        Item = item;

        //
        // For now we expect that MVC views and pages will still have either:
        // [RazorView(...)] or
        // [RazorPage(...)].
        //
        // In theory we could look at the 'Item.Kind' to determine what kind of thing we're dealing
        // with, but for compat reasons we're basing it on ViewAttribute since that's what 2.0 had.
#pragma warning disable CS0618 // Type or member is obsolete
        ViewAttribute = attribute;
#pragma warning restore CS0618 // Type or member is obsolete

        // We don't have access to the file provider here so we can't check if the files
        // even exist or what their checksums are. For now leave this empty, it will be updated
        // later.
        RelativePath = ViewPath.NormalizePath(item?.Identifier ?? attribute.Path);
    }
コード例 #2
0
        public void PopulateFeature_PopulatesRazorViewAttributeFromTypeAssembly()
        {
            // Arrange
            var item1 = Mock.Of <RazorCompiledItem>(i => i.Identifier == "Item1" && i.Type == typeof(TestView));
            var item2 = Mock.Of <RazorCompiledItem>(i => i.Identifier == "Item2" && i.Type == typeof(TestPage));

            var attribute1 = new RazorViewAttribute("Item1", typeof(TestView));
            var attribute2 = new RazorViewAttribute("Item2", typeof(TestPage));

            var assembly = new TestAssembly(new[] { attribute1, attribute2 });

            var part1 = new AssemblyPart(assembly);
            var part2 = new Mock <ApplicationPart>();

            part2
            .As <IRazorCompiledItemProvider>()
            .Setup(p => p.CompiledItems).Returns(new[] { item1, item2, });
            var featureProvider = new RazorCompiledItemFeatureProvider();
            var feature         = new ViewsFeature();

            // Act
            featureProvider.PopulateFeature(new[] { part1, part2.Object }, feature);

            // Assert
            Assert.Equal(new[] { item1, item2 }, feature.ViewDescriptors.Select(d => d.Item));
        }
コード例 #3
0
        // Token: 0x06000181 RID: 385 RVA: 0x00007290 File Offset: 0x00005490
        protected virtual CompiledViewDescriptor CompileAndEmit(string relativePath)
        {
            RazorProjectItem    item = this._projectEngine.FileSystem.GetItem(relativePath);
            RazorCodeDocument   razorCodeDocument = this._projectEngine.Process(item);
            RazorCSharpDocument csharpDocument    = RazorCodeDocumentExtensions.GetCSharpDocument(razorCodeDocument);

            if (csharpDocument.Diagnostics.Count > 0)
            {
                //throw CompilationFailedExceptionFactory.Create(razorCodeDocument, csharpDocument.Diagnostics);
                throw new ApplicationException("csharpDocument.Diagnostics.Count > 0");
            }
            Assembly           assembly        = this.CompileAndEmit(razorCodeDocument, csharpDocument.GeneratedCode);
            RazorCompiledItem  item2           = new RazorCompiledItemLoader().LoadItems(assembly).SingleOrDefault <RazorCompiledItem>();
            RazorViewAttribute customAttribute = assembly.GetCustomAttribute <RazorViewAttribute>();

            return(new CompiledViewDescriptor(item2, customAttribute));
        }