Search code examples
c#.netasp.net-identity

#nullable disable - only for specific namespace?


In my asp.net core project I turned on nullable warnings with:

<Nullable>enable</Nullable>

Everything works just fine. But after adding scaffolded items from Identity (like Login.cshtml, Register.cshtml) many of these warning showed up. I don't want to touch that (somebody else's) code (but I need it for some reason).

I can use #nullable disable in every file, but I was thinking that maybe better solution exists? Restricting #nullable disable only to specific namespace??


Solution

  • While you cannot disable nullable checks on a per-namespace basis, you can customize the severity of issues which those checks surface, via your .editorconfig file.

    Many projects make their folder structure mirror their namespaces. If this is the case for your project, you can either put an .editorconfig file into the folder at the root of your target namespace, or you can create a file matching pattern in another .editorconfig file further up the folder tree to target items in that folder. For example:

    [**/Models/**.cs]
    # API models will be serialized and deserialized, so they can have default constructors,
    # but we assume the required values aren't null anyway.
    dotnet_diagnostic.CS8618.severity = none