question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SA1649 conflicts with default Razor Pages file/class naming

See original GitHub issue

When creating a Razor Page it will generate a .cshtml and .cshtml.cs files. The model used inside this file is postfixed with Model, which causes SA1649 to be violated.

Eg: When creating a Razor Page called Index, the files generated will be called Index.cshtml and Index.cshtml.cs, but the class generated will be called IndexModel.

Not sure if this case should be allowed in SA1649, as I don’t want to switch this rule off for the whole project and adding #pragma warning disable/restore to every Razor Page seems excessive to me?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
sharwellcommented, Aug 5, 2019

@daviddunson That should be possible using .editorconfig in 16.3.

[*.cshtml.cs]
dotnet_diagnostic.SA1649.severity = none
1reaction
af4jmcommented, Jul 5, 2018

@sharwell

I know I’m late to the party here, but your suggestion #2 would break the convention that’s been in place since the beginning of MVC where model classes end with the Model suffix to distinguish from similarly named objects (usually entities), #3 requires also renaming the .cshtml file, which is part of the URL.

The gotcha here is that in traditional MVC, the cshtml files had no code-behind & the models live in a separate folder… now with Razor Pages the model is the code-behind, which for many scenarios is much cleaner by having related pieces together, but does break this rule as the two long-standing conventions now conflict with each other.

It would be nice if rulesets could apply by subfolder instead of strictly at the project level, but I have a hunch the amount of effort required to make that happen means I’m not likely to see it anytime soon.

Meantime, as ugly as it is, I’m setting my convention to be the first line of all Razor Page code behinds/models is #pragma warning disable SA1649.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Disable SA1649 for file names that contain a period · Issue ...
Given a class "ClassName" and a filename of "ClassName.Generated.cs", I think this should be valid for the rule. I believe it's a common ......
Read more >
Razor page namespace conflicts with class library
I have a razor page with a namespace like MyApp.Pages.MyClass . Within this page cshtml I am trying to reference an enum in...
Read more >
Changing the Default Page in ASP.NET Core Razor Pages
Hello, After I created some Razor Pages in our app, I changed Root directory from Pages to My. Now how can I change...
Read more >
If my solution is totally based on razor page and partial ...
I'm assuming here that your razor pages are not conflicting with the controllers. If they are then the wrong code is getting called....
Read more >
Razor Pages for ASP.NET Core - Full Course (.NET 6)
Learn the basics of Razor Pages for ASP. ... Custom Validations ⌨️ (2:09:38) Display Name and Range Annotation ⌨️ (2:12:58) Client Side ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found