Razor pages with runtime compilation enabled throw exception when creating models using the C# 11 'required' keyword
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the bug
When Razor runtime compilation is enabled, it is not possible to construct models that use the C# 11 `required` keyword from a razor page, though it works fine everywhere else.
Once the code is executed, the following exception is thrown:
```
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.CompilationFailedException: One or more compilation failures occurred:
C:\Code\Learning\RazorRequiredField\Pages\Index.cshtml(13,20): error CS0619: 'SampleModel.SampleModel()' is obsolete: 'Constructors of types with required members are not supported in this version of your compiler.'
at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.CompileAndEmit(RazorCodeDocument codeDocument, String generatedCode)
at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.CompileAndEmit(String relativePath)
at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.OnCacheMiss(String normalizedPath)
```
### Expected Behavior
The expected behaviour is that these new features can be used in pages, as well as anywhere else.
### Steps To Reproduce
A full example project that exhibits this behavior can be found here:
https://github.com/NinovanderMark/RazorPagesRuntimeCompilationBug
To create it from scratch, simply create a new .NET 7 Web application using Razor Pages, and enable runtime compilation like so:
```csharp
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
```
Next, add a new model which has a `required` field with. Something like this:
```csharp
public class SampleModel
{
public required string Name { get; init; }
}
```
Finally, instantiate this class from a Razor Page. Maybe we have a Method that requires it, so we call that with a newly constructed instance:
```csharp
@Model.Method(new SampleModel { Name = "SomethingTrue" })
```
Once the user navigates to the page that has this code, the exception will be thrown.
### Exceptions (if any)
```
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.CompilationFailedException: One or more compilation failures occurred:
C:\Code\Learning\RazorRequiredField\Pages\Index.cshtml(13,20): error CS0619: 'SampleModel.SampleModel()' is obsolete: 'Constructors of types with required members are not supported in this version of your compiler.'
at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.CompileAndEmit(RazorCodeDocument codeDocument, String generatedCode)
at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.CompileAndEmit(String relativePath)
at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.OnCacheMiss(String normalizedPath)
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DefaultPageLoader.LoadAsyncCore(PageActionDescriptor actionDescriptor, EndpointMetadataCollection endpointMetadata)
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageLoaderMatcherPolicy.ApplyAsyncAwaited(PageLoader pageLoader, CandidateSet candidates, Task`1 actionDescriptorTask, Int32 index)
at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.SelectEndpointWithPoliciesAsync(HttpContext httpContext, IEndpointSelectorPolicy[] policies, CandidateSet candidateSet)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.g__AwaitMatch|8_1(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matchTask)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
```
### .NET Version
7.0.100
### Anything else?
This was tested with version `7.0.1` of the `Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation` package, running the project from either Visual Studio 2022, or JetBrains Rider.
Contributor guide
Assessment
This issue has not been assessed yet.