Skip to content

Do not report SA1649 for Razor page code-behind files - #86

Merged
bjornhellander merged 1 commit into
bjornhellander:masterfrom
MattFromRVA:SA1649_RazorPages
Aug 3, 2026
Merged

Do not report SA1649 for Razor page code-behind files#86
bjornhellander merged 1 commit into
bjornhellander:masterfrom
MattFromRVA:SA1649_RazorPages

Conversation

@MattFromRVA

@MattFromRVA MattFromRVA commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #38

SA1649 no longer applies to any file ending in .cshtml.cs. The name of a Razor page's code-behind file is dictated by its sibling .cshtml page, not by the type inside it, so the rule has no useful signal there and its code fix would rename the file and break the page.

This replaces the heuristic added in #19, which required the first type to be a class, its name to end in Model, and its first base type to be textually PageModel or *.PageModel. That missed page models deriving from a project-local base class, which is what #38 reports.

Behavior change: NonModelClassInCshtmlCsFileTriggersDiagnosticAsync asserted that a non-page-model class in a .cshtml.cs file does report. That expectation is now inverted (the IndexHandler theory row). The case it covered — a second, unrelated type declared first in the file — is still reported by SA1402, which is the more accurate diagnosis anyway.

@MattFromRVA
MattFromRVA marked this pull request as ready for review July 30, 2026 01:42
@MattFromRVA

Copy link
Copy Markdown
Contributor Author

@bjornhellander Ready for review

@bjornhellander

Copy link
Copy Markdown
Owner

My memory of this issue is a bit foggy and I see that I didn't describe my thoughts very clearly in it, so I would appreciate an explanation of what you are trying to accomplish with the PR and why. For example if it's only to make sure there's no codefix messing up if the files are oddly named, or if you have found cases where the current analyzer is triggering when it shouldn't so it needs to be relaxed further?

@MattFromRVA

Copy link
Copy Markdown
Contributor Author

@bjornhellander No worries. Let me put in a proper description for this PR.
It's both of your options, because they turn out to be the same defect: the code fix can only rename a .cshtml.cs file if the analyzer reported on it first, so relaxing the analyzer makes the bad rename unreachable without touching the fix provider.

And yes, I found it triggering on a normally-named file, not just an oddly-named one. #19's suppression required the first base type to be textually PageModel or *.PageModel, so this gets flagged:

// Index.cshtml.cs
public class IndexModel : MyBasePageModel   // MyBasePageModel : PageModel
{
}

So I made SA1649 skip any file ending in .cshtml.cs

@bjornhellander

Copy link
Copy Markdown
Owner

The false positive when using a base class could have been handled in another way, but I agree that just skipping these files is probably for the best.

@bjornhellander
bjornhellander merged commit 34ea163 into bjornhellander:master Aug 3, 2026
20 checks passed
@MattFromRVA
MattFromRVA deleted the SA1649_RazorPages branch August 22, 2026 02:46
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.

SA1649 handling of Razor pages could possibly be improved

2 participants