Skip to content

Add SuppressMvcRazorImports configuration option#12849

Draft
DamianEdwards wants to merge 1 commit intomainfrom
dev/damian/suppress-mvc-razor-imports
Draft

Add SuppressMvcRazorImports configuration option#12849
DamianEdwards wants to merge 1 commit intomainfrom
dev/damian/suppress-mvc-razor-imports

Conversation

@DamianEdwards
Copy link
Member

Summary

Adds a new SuppressMvcRazorImports MSBuild property that, when set to true, suppresses all MVC-specific default directives from the synthetic import in MvcImportProjectFeature.

Fixes #8259

Details

When enabled, the default import retains only the System @using directives:

@using global::System
@using global::System.Collections.Generic
@using global::System.Linq
@using global::System.Threading.Tasks

The following 11 MVC-specific directives are suppressed:

  • 3 @using Microsoft.AspNetCore.Mvc.* directives
  • 5 @inject directives (IHtmlHelper, IJsonHelper, IViewComponentHelper, IUrlHelper, IModelExpressionProvider)
  • 3 @addTagHelper directives (UrlResolutionTagHelper, HeadTagHelper, BodyTagHelper)

Explicit @inject, @using, and @addTagHelper directives in .cshtml or _ViewImports.cshtml files continue to work regardless of this setting.

Usage

<PropertyGroup>
  <SuppressMvcRazorImports>true</SuppressMvcRazorImports>
</PropertyGroup>

Follow-up needed

A change in dotnet/sdk is needed to add <CompilerVisibleProperty Include="SuppressMvcRazorImports" /> so this property flows from MSBuild to the source generator. Until then, users can set it via .globalconfig:

build_property.SuppressMvcRazorImports = true

Changes

File Change
RazorConfiguration.cs New SuppressMvcRazorImports bool parameter + equality/hash
MvcImportProjectFeature.cs Conditional default import (full MVC vs System-only)
RazorSourceGenerator.RazorProviders.cs Reads build_property.SuppressMvcRazorImports
RazorProjectInfoFactory.cs Same for language server path
RazorConfigurationFormatter.cs MessagePack serialization
ObjectWriters.cs / ObjectReaders.cs JSON serialization
MvcImportProjectFeatureTest.cs 2 new unit tests
RazorSourceGeneratorTests.cs E2E source generator test
RazorConfigurationSerializationTest.cs Round-trip test updated

Add a new SuppressMvcRazorImports boolean property to RazorConfiguration
that, when set to true via MSBuild property, suppresses all MVC-specific
default directives from the synthetic import in MvcImportProjectFeature.

When enabled, the default import retains only System @using directives.
The following MVC-specific directives are suppressed:
- 3 @using Microsoft.AspNetCore.Mvc.* directives
- 5 @Inject directives (IHtmlHelper, IJsonHelper, etc.)
- 3 @addTagHelper directives (UrlResolution, Head, Body)

Explicit @Inject, @using, and @addTagHelper directives in .cshtml or
_ViewImports.cshtml files continue to work regardless of this setting.

Users set <SuppressMvcRazorImports>true</SuppressMvcRazorImports> in
their project file. A follow-up change in dotnet/sdk is needed to add
the corresponding CompilerVisibleProperty declaration.

Fixes #8259

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Disable generation of default RazorInjected properties

1 participant