Skip to content

A reusable _Imports.razor or <Using /> for Razor Components #64438

@datvm

Description

@datvm

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Currently, the only global using equivalent for Razor is the _Imports.razor which unfortunately is not reusable or even modular in anyway. Meanwhile, C# has serveral ways: global using from multiple files, <Using Include="namespace" /> which can also be reused by using <Import /> from targets.

Describe the solution you'd like

  1. Razor Components should honor <Using /> when generating component. To prevent breaking change, maybe it should be opt-in with a property switch:
	<PropertyGroup>
		<RazorUsesUsings>true</RazorUsesUsings>
	</PropertyGroup>

	<ItemGroup>
		<Using Include="Microsoft.AspNetCore.Components" />
		<Using Include="Microsoft.AspNetCore.Components.Forms" />
		<Using Include="Microsoft.AspNetCore.Components.Web" />
	</ItemGroup>

At first I thought about a new tag like RazorUsing but I think it's a bad idea because in the end it mostly will just repeat whatever Using there are.

  1. A reusable _Imports.razor component. I am not sure how feasible it should be, but it could be like this:

Common1.razor

<Using Namespace="Microsoft.AspNetCore.Components" />
<Using Namespace="Microsoft.AspNetCore.Components.Forms" />

_Imports.razor

<Namespace.Common1 />
<Namespace.Common2 />

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions