dotnet / dotnet/aspnetcore

[Blazor] Consider a build-time analyzer for form method=dialog / non-default target instead of the runtime enhanced-navigation warning

Open
#67,676 0 comments 0 reactions 0 assignees View on GitHub
analyzer area-blazor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

## Summary

`src/Components/Web.JS/src/Services/NavigationEnhancement.ts`'s `onDocumentSubmit` handler currently detects two cases where a form cannot be enhanced, and logs a runtime `console.warn` for each:

```ts
const method = event.submitter?.getAttribute('formmethod') || formElem.method;
if (method === 'dialog') {
console.warn('A form cannot be enhanced when its method is "dialog".');
return;
}

const target = event.submitter?.getAttribute('formtarget') || formElem.target;
if (target !== '' && target !== '_self') {
console.warn('A form cannot be enhanced when its target is different from the default value "_self".');
return;
}
```

Both are genuine functional issues (the form silently falls back to a full page load instead of being enhanced), not style preferences, so a warning is justified. However, both conditions are driven by static attributes on the `` / submit button (`method` / `formmethod`, `target` / `formtarget`) that are almost always known at compile time from the `.razor` markup.

## Ask

Investigate whether a build-time Roslyn analyzer (in `Microsoft.AspNetCore.Components.Analyzers`) can detect these same two cases directly from `.razor` markup: a `` (or ``) with a literal `method="dialog"`, or a literal `target` / `formtarget` value other than `""` / `"_self"`, combined with enhanced navigation being enabled (the default, i.e. not opted out via `data-enhance-nav="false"` or similar).

If feasible, this would let developers catch the misconfiguration in the editor / at compile time instead of only discovering it at runtime via the console warning, the same motivation discussed for the Virtualize spacer-element case.

## Constraint

The analyzer can only fire when the value is a static literal. Dynamic or data-bound `method` / `target` / `formmethod` / `formtarget` values (e.g. `method="@someExpression"`) cannot be analyzed statically, so the runtime `console.warn` must remain as the fallback for those cases.

Contributor guide

Open the contributing guide

Research direction

Start in Microsoft.AspNetCore.Components.Analyzers and review the existing Virtualize spacer-element analyzer approach. Compare its possible Razor markup inputs with the onDocumentSubmit handler in src/Components/Web.JS/src/Services/NavigationEnhancement.ts. Done means determining whether static method/target values can be diagnosed when enhanced navigation is enabled, while dynamic values remain covered by the runtime warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, typescript
Domain
tooling, web-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.