dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
505 stars 195 forks source link

Help Blazor users correctly setup the Microsoft.AspNetCore.Components.Web using directive #9747

Open danroth27 opened 1 year ago

danroth27 commented 1 year ago

A using directive for the Microsoft.AspNetCore.Components.Web namespace is required to get the web related Razor directives to work (like @onclick and friends). We add this using directive in our templates, but occasionally users still get tripped up by not having it. For example, I helped someone who was following the Blazor Hybrid tutorials for WPF & WinForms, but they messed up the name of their _Imports.razor file. The app compiled and ran but a bunch of the Razor directives didn't work. I've seen users also start with a Razor Class Library configured with Pages & View and then try to add Blazor components to it run into this problem.

Can we provide better diagnostics for this situation, where users have the web related Razor directives in their code, but they are missing the using directive?

Or could we add this using directive as an implicit using for the Razor SDK? Or maybe just the Web and Blazor WebAssembly SDKs?

javiercn commented 1 year ago

@danroth27 would this be a tooling/compiler ask? (Like a warning/analyzer).

I doubt we would want to put something inside the runtime for this.

danroth27 commented 1 year ago

@danroth27 would this be a tooling/compiler ask? (Like a warning/analyzer). I doubt we would want to put something inside the runtime for this.

That's my thinking too. But I filed the issue here first so that we could discuss the right way to approach this problem in triage.

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

davidwengier commented 11 months ago

Moving this to compiler for a first look (though maybe its an LDM thing?).

Or could we add this using directive as an implicit using for the Razor SDK? Or maybe just the Web and Blazor WebAssembly SDKs?

I think this makes heaps of sense, even if its just the compiler always emiting a using directive based on language version or something. Personally I think _Imports.razor as a concept could stand to disappear now that we have implicit usings in the SDK, and global usings in C#. But that might be too bold 😁