Fix #5116 Remove support for 2.0 precompilation

Obsoletes RazorViewAttribute and RazorPageAttribute and removes all of
our usage of them. I've already made the changes to have the SDK stop
generating these.
This commit is contained in:
Ryan Nowak 2019-01-04 14:45:18 -08:00
parent 5299eff616
commit 14e606997f
23 changed files with 124 additions and 336 deletions

View File

@ -57,7 +57,7 @@
<MicrosoftIdentityModelClientsActiveDirectoryPackageVersion>3.19.8</MicrosoftIdentityModelClientsActiveDirectoryPackageVersion>
<MicrosoftIdentityModelProtocolsOpenIdConnectPackageVersion>5.3.0</MicrosoftIdentityModelProtocolsOpenIdConnectPackageVersion>
<MicrosoftNETCoreApp30PackageVersion>3.0.0-preview1-26907-05</MicrosoftNETCoreApp30PackageVersion>
<MicrosoftNETSdkRazorPackageVersion>3.0.0-preview-18579-0056</MicrosoftNETSdkRazorPackageVersion>
<MicrosoftNETSdkRazorPackageVersion>3.0.0-preview.19053.2</MicrosoftNETSdkRazorPackageVersion>
<MicrosoftNETTestSdkPackageVersion>15.6.1</MicrosoftNETTestSdkPackageVersion>
<MicrosoftOwinSecurityCookiesPackageVersion>3.0.1</MicrosoftOwinSecurityCookiesPackageVersion>
<MicrosoftOwinSecurityInteropPackageVersion>3.0.0-alpha1-10772</MicrosoftOwinSecurityInteropPackageVersion>

View File

@ -2,7 +2,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28414.68
MinimumVisualStudioVersion = 15.0.26730.03
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{DAAE4C74-D06F-4874-A166-33305D2643CE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{32285FA4-6B46-4D6B-A840-2B13E4C8B58E}"
@ -148,8 +148,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmarks", "benchmarks",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RazorBuildWebSite", "test\WebSites\RazorBuildWebSite\RazorBuildWebSite.csproj", "{BF8A3392-C3D2-4813-855A-E906564600E1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RazorBuildWebSite.PrecompiledViews", "test\WebSites\RazorBuildWebSite.PrecompiledViews\RazorBuildWebSite.PrecompiledViews.csproj", "{856D7E25-E033-477D-9ABD-0B50CF428C80}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RazorBuildWebSite.Views", "test\WebSites\RazorBuildWebSite.Views\RazorBuildWebSite.Views.csproj", "{8916DDCA-EC2A-4193-B9F3-78CAA1A96D5A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Mvc.Analyzers", "src\Microsoft.AspNetCore.Mvc.Analyzers\Microsoft.AspNetCore.Mvc.Analyzers.csproj", "{87A3E227-C45E-4141-A59F-402908E651FD}"
@ -868,18 +866,6 @@ Global
{BF8A3392-C3D2-4813-855A-E906564600E1}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{BF8A3392-C3D2-4813-855A-E906564600E1}.Release|x86.ActiveCfg = Release|Any CPU
{BF8A3392-C3D2-4813-855A-E906564600E1}.Release|x86.Build.0 = Release|Any CPU
{856D7E25-E033-477D-9ABD-0B50CF428C80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{856D7E25-E033-477D-9ABD-0B50CF428C80}.Debug|Any CPU.Build.0 = Debug|Any CPU
{856D7E25-E033-477D-9ABD-0B50CF428C80}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{856D7E25-E033-477D-9ABD-0B50CF428C80}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{856D7E25-E033-477D-9ABD-0B50CF428C80}.Debug|x86.ActiveCfg = Debug|Any CPU
{856D7E25-E033-477D-9ABD-0B50CF428C80}.Debug|x86.Build.0 = Debug|Any CPU
{856D7E25-E033-477D-9ABD-0B50CF428C80}.Release|Any CPU.ActiveCfg = Release|Any CPU
{856D7E25-E033-477D-9ABD-0B50CF428C80}.Release|Any CPU.Build.0 = Release|Any CPU
{856D7E25-E033-477D-9ABD-0B50CF428C80}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{856D7E25-E033-477D-9ABD-0B50CF428C80}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{856D7E25-E033-477D-9ABD-0B50CF428C80}.Release|x86.ActiveCfg = Release|Any CPU
{856D7E25-E033-477D-9ABD-0B50CF428C80}.Release|x86.Build.0 = Release|Any CPU
{8916DDCA-EC2A-4193-B9F3-78CAA1A96D5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8916DDCA-EC2A-4193-B9F3-78CAA1A96D5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8916DDCA-EC2A-4193-B9F3-78CAA1A96D5A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@ -1634,7 +1620,6 @@ Global
{F16CEE0D-A28E-43BD-802F-99BAFE4BA7CE} = {FDC66952-A3EA-4074-899E-C29816BF7C1F}
{7500B228-1769-4CFB-A571-3DFAC6678A06} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
{BF8A3392-C3D2-4813-855A-E906564600E1} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{856D7E25-E033-477D-9ABD-0B50CF428C80} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{8916DDCA-EC2A-4193-B9F3-78CAA1A96D5A} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{87A3E227-C45E-4141-A59F-402908E651FD} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
{E3E09D2F-1FCF-4396-9B09-5A62CA8CC831} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1}

View File

@ -21,6 +21,23 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
}
/// <summary>
/// Creates a new <see cref="CompiledViewDescriptor"/>.
/// </summary>
/// <param name="item">The <see cref="RazorCompiledItem"/>.</param>
public CompiledViewDescriptor(RazorCompiledItem item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
Item = item;
ExpirationTokens = Array.Empty<IChangeToken>();
RelativePath = ViewPath.NormalizePath(item.Identifier);
}
#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>.
@ -28,6 +45,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
/// <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)
{
@ -44,7 +62,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
//
// 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
@ -58,13 +78,17 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
/// </summary>
public string RelativePath { get; set; }
#pragma warning disable CS0618
// Type or member is obsolete
/// <summary>
/// Gets or sets the <see cref="RazorViewAttribute"/> decorating the view.
/// </summary>
/// <remarks>
/// May be <c>null</c>.
/// </remarks>
[Obsolete("Use Item instead. RazorViewAttribute has been superseded by RazorCompiledItem and will not be used by the runtime.")]
public RazorViewAttribute ViewAttribute { get; set; }
#pragma warning restore CS0618 // Type or member is obsolete
/// <summary>
/// <see cref="IChangeToken"/> instances that indicate when this result has expired.
@ -79,6 +103,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
/// <summary>
/// Gets the type of the compiled item.
/// </summary>
public Type Type => Item?.Type ?? ViewAttribute?.ViewType;
public Type Type => Item?.Type;
}
}

View File

@ -5,6 +5,7 @@ using System;
namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
{
[Obsolete("This attribute has been superseded by RazorCompiledItem and will not be used by the runtime.")]
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public class RazorViewAttribute : Attribute
{

View File

@ -82,10 +82,6 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
{
return viewDescriptor.Item.Kind == RazorPageDocumentKind;
}
else if (viewDescriptor.ViewAttribute != null)
{
return viewDescriptor.ViewAttribute is RazorPageAttribute;
}
return false;
}
@ -133,11 +129,6 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
internal static string GetRouteTemplate(CompiledViewDescriptor viewDescriptor)
{
if (viewDescriptor.ViewAttribute != null)
{
return ((RazorPageAttribute)viewDescriptor.ViewAttribute).RouteTemplate;
}
if (viewDescriptor.Item != null)
{
return viewDescriptor.Item.Metadata

View File

@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc.Razor.Compilation;
namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{
[Obsolete("This attribute has been superseded by RazorCompiledItem and will not be used by the runtime.")]
public class RazorPageAttribute : RazorViewAttribute
{
public RazorPageAttribute(string path, Type viewType, string routeTemplate)

View File

@ -17,30 +17,6 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
public HttpClient Client { get; }
[Fact]
public async Task PrecompiledPage_LocalPageWithDifferentContent_NotUsed()
{
// Act
var response = await Client.GetAsync("http://localhost/Precompilation/Page");
var responseBody = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("Hello from buildtime-compiled precompilation page!", responseBody.Trim());
}
[Fact]
public async Task PrecompiledView_LocalViewWithDifferentContent_NotUsed()
{
// Act
var response = await Client.GetAsync("http://localhost/Precompilation/View");
var responseBody = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("Hello from buildtime-compiled precompilation view!", responseBody.Trim());
}
[Fact(Skip = "https://github.com/aspnet/Mvc/issues/8753")]
public async Task Rzc_LocalPageWithDifferentContent_IsUsed()
{
@ -66,7 +42,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
}
[Fact]
public async Task RzcViewsArePreferredToPrecompiledViews()
public async Task RzcViewsArePreferredToRuntimeViews()
{
// Verifies that when two views have the same paths, the one compiled using rzc is preferred to the one from Precompilation.
// Act

View File

@ -41,16 +41,18 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts
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 assembly = new TestAssembly(new[]
{
new RazorCompiledItemAttribute(typeof(TestView), "mvc.1.0.razor-page", "Item1"),
new RazorCompiledItemAttribute(typeof(TestView), "mvc.1.0.razor-view", "Item1"),
});
var part1 = new AssemblyPart(assembly);
var part2 = new Mock<ApplicationPart>();
part2
.As<IRazorCompiledItemProvider>()
.Setup(p => p.CompiledItems).Returns(new[] { item1, item2, });
.Setup(p => p.CompiledItems)
.Returns(new[] { item1, item2, });
var featureProvider = new RazorCompiledItemFeatureProvider();
var feature = new ViewsFeature();

View File

@ -3,6 +3,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Hosting;
using Microsoft.Extensions.Primitives;
using Moq;
using Xunit;
@ -54,7 +55,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
var descriptor = new CompiledViewDescriptor
{
RelativePath = relativePath,
ViewAttribute = new RazorViewAttribute(relativePath, typeof(TestRazorPage)),
Item = TestRazorCompiledItem.CreateForView(typeof(TestRazorPage), relativePath),
ExpirationTokens = expirationTokens,
};
var compilerCache = new Mock<IViewCompiler>();
@ -80,7 +81,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
var descriptor = new CompiledViewDescriptor
{
RelativePath = relativePath,
ViewAttribute = new RazorViewAttribute(relativePath, typeof(TestRazorPage)),
Item = TestRazorCompiledItem.CreateForView(typeof(TestRazorPage), relativePath),
ExpirationTokens = Array.Empty<IChangeToken>(),
};
var viewCompiler = new Mock<IViewCompiler>();

View File

@ -23,7 +23,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
var result2 = await viewCompiler.CompileAsync(path);
// Assert
Assert.Null(result1.ViewAttribute);
Assert.Empty(result1.ExpirationTokens);
}

View File

@ -22,67 +22,16 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
public void OnProvidersExecuting_AddsModelsForCompiledViews()
{
// Arrange
var descriptors = new[]
var items = new[]
{
CreateVersion_2_0_Descriptor("/Pages/About.cshtml"),
CreateVersion_2_0_Descriptor("/Pages/Home.cshtml", "some-prefix"),
};
var provider = CreateProvider(descriptors: descriptors);
var context = new PageRouteModelProviderContext();
// Act
provider.OnProvidersExecuting(context);
// Assert
Assert.Collection(
context.RouteModels,
result =>
{
Assert.Equal("/Pages/About.cshtml", result.RelativePath);
Assert.Equal("/About", result.ViewEnginePath);
Assert.Collection(
result.Selectors,
selector => Assert.Equal("About", selector.AttributeRouteModel.Template));
Assert.Collection(
result.RouteValues.OrderBy(k => k.Key),
kvp =>
{
Assert.Equal("page", kvp.Key);
Assert.Equal("/About", kvp.Value);
});
},
result =>
{
Assert.Equal("/Pages/Home.cshtml", result.RelativePath);
Assert.Equal("/Home", result.ViewEnginePath);
Assert.Collection(
result.Selectors,
selector => Assert.Equal("Home/some-prefix", selector.AttributeRouteModel.Template));
Assert.Collection(
result.RouteValues.OrderBy(k => k.Key),
kvp =>
{
Assert.Equal("page", kvp.Key);
Assert.Equal("/Home", kvp.Value);
});
});
}
[Fact] // 2.1 adds some additional metadata to the view descriptors. We want to make sure both versions work.
public void OnProvidersExecuting_AddsModelsForCompiledViews_Version_2_1()
{
// Arrange
var descriptors = new[]
{
CreateVersion_2_1_Descriptor("/Pages/About.cshtml"),
CreateVersion_2_1_Descriptor("/Pages/Home.cshtml", metadata: new[]
TestRazorCompiledItem.CreateForPage("/Pages/About.cshtml"),
TestRazorCompiledItem.CreateForPage("/Pages/Home.cshtml", metadata: new[]
{
new RazorCompiledItemMetadataAttribute("RouteTemplate", "some-prefix"),
}),
};
var provider = CreateProvider(descriptors: descriptors);
var provider = CreateProvider(items);
var context = new PageRouteModelProviderContext();
// Act
@ -127,12 +76,15 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
public void OnProvidersExecuting_AddsModelsForCompiledAreaPages()
{
// Arrange
var descriptors = new[]
var items = new[]
{
CreateVersion_2_0_Descriptor("/Areas/Products/Files/About.cshtml"),
CreateVersion_2_0_Descriptor("/Areas/Products/Pages/About.cshtml"),
CreateVersion_2_0_Descriptor("/Areas/Products/Pages/Manage/Index.cshtml"),
CreateVersion_2_0_Descriptor("/Areas/Products/Pages/Manage/Edit.cshtml", "{id}"),
TestRazorCompiledItem.CreateForPage("/Areas/Products/Files/About.cshtml"),
TestRazorCompiledItem.CreateForPage("/Areas/Products/Pages/About.cshtml"),
TestRazorCompiledItem.CreateForPage("/Areas/Products/Pages/Manage/Index.cshtml"),
TestRazorCompiledItem.CreateForPage("/Areas/Products/Pages/Manage/Edit.cshtml", metadata: new object[]
{
new RazorCompiledItemMetadataAttribute("RouteTemplate", "{id}"),
}),
};
var options = new RazorPagesOptions
@ -141,7 +93,7 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
RootDirectory = "/Files",
};
var provider = CreateProvider(options: options, descriptors: descriptors);
var provider = CreateProvider(items, options);
var context = new PageRouteModelProviderContext();
// Act
@ -216,12 +168,12 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
public void OnProvidersExecuting_DoesNotAddAreaAndNonAreaRoutesForAPage()
{
// Arrange
var descriptors = new[]
var items = new[]
{
CreateVersion_2_0_Descriptor("/Areas/Accounts/Pages/Manage/Home.cshtml"),
CreateVersion_2_0_Descriptor("/Areas/Accounts/Manage/Home.cshtml"),
CreateVersion_2_0_Descriptor("/Areas/About.cshtml"),
CreateVersion_2_0_Descriptor("/Contact.cshtml"),
TestRazorCompiledItem.CreateForPage("/Areas/Accounts/Pages/Manage/Home.cshtml"),
TestRazorCompiledItem.CreateForPage("/Areas/Accounts/Manage/Home.cshtml"),
TestRazorCompiledItem.CreateForPage("/Areas/About.cshtml"),
TestRazorCompiledItem.CreateForPage("/Contact.cshtml"),
};
var options = new RazorPagesOptions
@ -229,7 +181,7 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
RootDirectory = "/",
};
var provider = CreateProvider(options: options, descriptors: descriptors);
var provider = CreateProvider(items, options);
var context = new PageRouteModelProviderContext();
// Act
@ -279,14 +231,17 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
public void OnProvidersExecuting_AddsMultipleSelectorsForIndexPage_WithIndexAtRoot()
{
// Arrange
var descriptors = new[]
var items = new[]
{
CreateVersion_2_0_Descriptor("/Pages/Index.cshtml"),
CreateVersion_2_0_Descriptor("/Pages/Admin/Index.cshtml", "some-template"),
TestRazorCompiledItem.CreateForPage("/Pages/Index.cshtml"),
TestRazorCompiledItem.CreateForPage("/Pages/Admin/Index.cshtml", metadata: new object[]
{
new RazorCompiledItemMetadataAttribute("RouteTemplate", "some-template"),
}),
};
var options = new RazorPagesOptions { RootDirectory = "/" };
var provider = CreateProvider(options: options, descriptors: descriptors);
var provider = CreateProvider(items, options);
var context = new PageRouteModelProviderContext();
// Act
@ -319,13 +274,16 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
public void OnProvidersExecuting_AddsMultipleSelectorsForIndexPage()
{
// Arrange
var descriptors = new[]
var items = new[]
{
CreateVersion_2_0_Descriptor("/Pages/Index.cshtml"),
CreateVersion_2_0_Descriptor("/Pages/Admin/Index.cshtml", "some-template"),
TestRazorCompiledItem.CreateForPage("/Pages/Index.cshtml"),
TestRazorCompiledItem.CreateForPage("/Pages/Admin/Index.cshtml", metadata: new object[]
{
new RazorCompiledItemMetadataAttribute("RouteTemplate", "some-template"),
}),
};
var provider = CreateProvider(descriptors: descriptors);
var provider = CreateProvider(items);
var context = new PageRouteModelProviderContext();
// Act
@ -358,13 +316,19 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
public void OnProvidersExecuting_AllowsRouteTemplatesWithOverridePattern()
{
// Arrange
var descriptors = new[]
var items = new[]
{
CreateVersion_2_0_Descriptor("/Pages/Index.cshtml", "~/some-other-prefix"),
CreateVersion_2_0_Descriptor("/Pages/Home.cshtml", "/some-prefix"),
TestRazorCompiledItem.CreateForPage("/Pages/Index.cshtml", metadata: new object[]
{
new RazorCompiledItemMetadataAttribute("RouteTemplate", "~/some-other-prefix"),
}),
TestRazorCompiledItem.CreateForPage("/Pages/Home.cshtml", metadata: new object[]
{
new RazorCompiledItemMetadataAttribute("RouteTemplate", "/some-prefix"),
}),
};
var provider = CreateProvider(descriptors: descriptors);
var provider = CreateProvider(items);
var context = new PageRouteModelProviderContext();
// Act
@ -399,16 +363,16 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
// to a Razor Page added by a library.
// Arrange
var descriptors = new[]
var items = new[]
{
// Page coming from the app
CreateVersion_2_1_Descriptor("/Pages/About.cshtml"),
CreateVersion_2_1_Descriptor("/Pages/Home.cshtml"),
TestRazorCompiledItem.CreateForPage("/Pages/About.cshtml"),
TestRazorCompiledItem.CreateForPage("/Pages/Home.cshtml"),
// Page coming from the app
CreateVersion_2_1_Descriptor("/Pages/About.cshtml"),
TestRazorCompiledItem.CreateForPage("/Pages/About.cshtml"),
};
var provider = CreateProvider(descriptors: descriptors);
var provider = CreateProvider(items);
var context = new PageRouteModelProviderContext();
// Act
@ -433,13 +397,13 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
public void OnProvidersExecuting_AllowsRazorFilesWithUnderscorePrefix()
{
// Arrange
var descriptors = new[]
var items = new[]
{
CreateVersion_2_1_Descriptor("/Pages/_About.cshtml"),
CreateVersion_2_1_Descriptor("/Pages/Home.cshtml"),
TestRazorCompiledItem.CreateForPage("/Pages/_About.cshtml"),
TestRazorCompiledItem.CreateForPage("/Pages/Home.cshtml"),
};
var provider = CreateProvider(descriptors: descriptors);
var provider = CreateProvider(items);
var context = new PageRouteModelProviderContext();
// Act
@ -480,42 +444,15 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
});
}
[Fact]
public void GetRouteTemplate_ReturnsPathFromRazorPageAttribute()
{
// Arrange
var expected = "test";
var descriptor = CreateVersion_2_0_Descriptor("/Pages/Home.cshtml", expected);
// Act
var result = CompiledPageRouteModelProvider.GetRouteTemplate(descriptor);
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void GetRouteTemplate_ReturnsNull_IfPageAttributeDoesNotHaveTemplate()
{
// Arrange
var descriptor = CreateVersion_2_0_Descriptor("/Pages/Home.cshtml", routeTemplate: null);
// Act
var result = CompiledPageRouteModelProvider.GetRouteTemplate(descriptor);
// Assert
Assert.Null(result);
}
[Fact]
public void GetRouteTemplate_ReturnsPathFromMetadataAttribute()
{
// Arrange
var expected = "test";
var descriptor = CreateVersion_2_1_Descriptor("/Pages/About.cshtml", metadata: new object[]
var descriptor = new CompiledViewDescriptor(TestRazorCompiledItem.CreateForPage("/Pages/About.cshtml", metadata: new object[]
{
new RazorCompiledItemMetadataAttribute("RouteTemplate", expected),
});
}));
// Act
var result = CompiledPageRouteModelProvider.GetRouteTemplate(descriptor);
@ -528,7 +465,7 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
public void GetRouteTemplate_ReturnsNull_IfAttributeDoesNotExist()
{
// Arrange
var descriptor = CreateVersion_2_1_Descriptor("/Pages/About.cshtml");
var descriptor = new CompiledViewDescriptor(TestRazorCompiledItem.CreateForPage("/Pages/About.cshtml"));
// Act
var result = CompiledPageRouteModelProvider.GetRouteTemplate(descriptor);
@ -537,9 +474,7 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
Assert.Null(result);
}
private TestCompiledPageRouteModelProvider CreateProvider(
RazorPagesOptions options = null,
IList<CompiledViewDescriptor> descriptors = null)
private CompiledPageRouteModelProvider CreateProvider(IList<RazorCompiledItem> items, RazorPagesOptions options = null)
{
options = options ?? new RazorPagesOptions();
@ -548,31 +483,14 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
Options.Create(options),
NullLogger<CompiledPageRouteModelProvider>.Instance);
provider.Descriptors.AddRange(descriptors ?? Array.Empty<CompiledViewDescriptor>());
for (var i = 0; i < items.Count; i++)
{
provider.Descriptors.Add(new CompiledViewDescriptor(items[i]));
}
return provider;
}
private static CompiledViewDescriptor CreateVersion_2_0_Descriptor(string path, string routeTemplate = "")
{
return new CompiledViewDescriptor
{
RelativePath = path,
ViewAttribute = new RazorPageAttribute(path, typeof(object), routeTemplate),
};
}
private static CompiledViewDescriptor CreateVersion_2_1_Descriptor(
string path,
object[] metadata = null)
{
return new CompiledViewDescriptor
{
RelativePath = path,
Item = new TestRazorCompiledItem(typeof(object), "mvc.1.0.razor-page", path, metadata ?? Array.Empty<object>()),
};
}
private class TestCompiledPageRouteModelProvider : CompiledPageRouteModelProvider
{
public TestCompiledPageRouteModelProvider(

View File

@ -5,6 +5,7 @@ using System;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Razor.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
@ -493,7 +494,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{
var descriptor = new CompiledViewDescriptor
{
ViewAttribute = new RazorPageAttribute("/Views/Index.cshtml", typeof(object), null),
Item = TestRazorCompiledItem.CreateForView(typeof(object), "/Views/Index.cshtml"),
};
var compiler = new Mock<IViewCompiler>();

View File

@ -10,12 +10,32 @@ namespace Microsoft.AspNetCore.Razor.Hosting
{
public class TestRazorCompiledItem : RazorCompiledItem
{
public static RazorCompiledItem CreateForPage(string identifier, object[] metadata = null)
{
return CreateForPage(type: null, identifier, metadata);
}
public static RazorCompiledItem CreateForPage(Type type, string identifier, object[] metadata = null)
{
return new TestRazorCompiledItem(type, "mvc.1.0.razor-page", identifier, metadata);
}
public static RazorCompiledItem CreateForView(string identifier, object[] metadata = null)
{
return CreateForView(type: null, identifier, metadata);
}
public static RazorCompiledItem CreateForView(Type type, string identifier, object[] metadata = null)
{
return new TestRazorCompiledItem(type, "mvc.1.0.razor-page", identifier, metadata);
}
public TestRazorCompiledItem(Type type, string kind, string identifier, object[] metadata)
{
Type = type;
Kind = kind;
Identifier = identifier;
Metadata = metadata;
Metadata = metadata ?? Array.Empty<object>();
}
public override string Identifier { get; }

View File

@ -1,39 +0,0 @@
#pragma checksum "D:\k\Mvc\test\WebSites\RazorBuildWebSite\Pages\Precompilation\Page.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "f3a9fa49018f90b3470f6c0e4f475d1d8c9cd456"
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/Pages/Precompilation/Page.cshtml", typeof(RazorBuildWebSite.Pages.Precompilation._Pages_Precompilation_Page), null)]
namespace RazorBuildWebSite.Pages.Precompilation
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
public class _Pages_Precompilation_Page : global::Microsoft.AspNetCore.Mvc.RazorPages.Page
{
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
BeginContext(26, 52, true);
WriteLiteral("\r\nHello from buildtime-compiled precompilation page!\r\n");
EndContext();
}
#pragma warning restore 1998
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; }
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; }
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; }
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; }
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<Page_Model> Html { get; private set; }
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<Page_Model> ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<Page_Model>)PageContext?.ViewData;
public Page_Model Model => ViewData.Model;
}
}
#pragma warning restore 1591

View File

@ -1,9 +0,0 @@

using Microsoft.AspNetCore.Mvc.RazorPages;
namespace RazorBuildWebSite.Pages.Precompilation
{
public class Page_Model : PageModel
{
}
}

View File

@ -1,37 +0,0 @@
#pragma checksum "D:\k\Mvc\test\WebSites\RazorBuildWebSite\Views\Common\CommonView.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "a09a0106df2e63aecf6fc6ddf30df39b489d9783"
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/Views/Common/CommonView.cshtml", typeof(RazorBuildWebSite.Views.Precompilation._Views_Precompilation_CommonView))]
namespace RazorBuildWebSite.Views.Precompilation
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
public class _Views_Precompilation_CommonView : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<dynamic>
{
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
BeginContext(0, 48, true);
WriteLiteral("Hello from buildtime-compiled precompilation view!");
EndContext();
}
#pragma warning restore 1998
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; }
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; }
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; }
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; }
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get; private set; }
}
}
#pragma warning restore 1591

View File

@ -1,37 +0,0 @@
#pragma checksum "D:\k\Mvc\test\WebSites\RazorBuildWebSite\Views\Precompilation\View.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "a09a0106df2e63aecf6fc6ddf30df39b489d9783"
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/Views/Precompilation/View.cshtml", typeof(RazorBuildWebSite.Views.Precompilation._Views_Precompilation_View))]
namespace RazorBuildWebSite.Views.Precompilation
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
public class _Views_Precompilation_View : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<dynamic>
{
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
BeginContext(0, 48, true);
WriteLiteral("Hello from buildtime-compiled precompilation view!");
EndContext();
}
#pragma warning restore 1998
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; }
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; }
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; }
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; }
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get; private set; }
}
}
#pragma warning restore 1591

View File

@ -2,7 +2,6 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(RazorBuildWebSite.Pages.Rzc.Pages_Rzc_Page), @"mvc.1.0.razor-page", @"/Pages/Rzc/Page.cshtml")]
[assembly:global::Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.RazorPageAttribute(@"/Pages/Rzc/Page.cshtml", typeof(RazorBuildWebSite.Pages.Rzc.Pages_Rzc_Page), null)]
namespace RazorBuildWebSite.Pages.Rzc
{
#line hidden

View File

@ -2,7 +2,6 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(RazorBuildWebSite.Views.Rzc.Views_Rzc_View), @"mvc.1.0.view", @"/Views/Common/CommonView.cshtml")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/Views/CommonView.cshtml", typeof(RazorBuildWebSite.Views.Rzc.Views_Rzc_View))]
namespace RazorBuildWebSite.Views.Rzc
{
#line hidden

View File

@ -2,7 +2,6 @@
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(RazorBuildWebSite.Views.Rzc.Views_Rzc_View), @"mvc.1.0.view", @"/Views/Rzc/View.cshtml")]
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/Views/Rzc/View.cshtml", typeof(RazorBuildWebSite.Views.Rzc.Views_Rzc_View))]
namespace RazorBuildWebSite.Views.Rzc
{
#line hidden

View File

@ -1,4 +0,0 @@
@page
@model Page_Model
Hello from runtime-compiled precompilation page!

View File

@ -11,8 +11,7 @@
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Mvc" />
<!-- Faking like we had Razor-on-Build (Rzc) and MvcPrecompilation -->
<ProjectReference Include="..\RazorBuildWebSite.PrecompiledViews\RazorBuildWebSite.PrecompiledViews.csproj" />
<!-- Faking like we had Razor-on-Build (RZC) -->
<ProjectReference Include="..\RazorBuildWebSite.Views\RazorBuildWebSite.Views.csproj" />
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />

View File

@ -1 +0,0 @@
Hello from runtime-compiled precompilation view!