14

I have a really simple ASP.Net WebAPI project created in .Net 6. Given this controller method:

[HttpPost]
public async Task DoStuff(MyClass input)
{
   // snip
}

where MyClass looks like this:

public class MyClass
{
    public string MyData { get; set; }
}

Posting this to the DoStuff method used to be allowed in previous versions of ASP.Net:

{
    MyData: null
}

Now however, it gives a 400 response unless I declare MyData as a string? instead of a string. My problem is that the MyClass class can not be altered, so I can't update MyData to be of type string?. Is there a way to disable the automatic null validation that ASP.Net does on MyClass properties? Adding <Nullable>disable</Nullable> to the csproj file for the WebAPI project doesn't seem to do anything. My current csproj looks like this:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Platforms>x64</Platforms>
    <Nullable>disable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <NoWarn>1701;1702;1591</NoWarn>
    <UserSecretsId>MyProject</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\AnotherProject.csproj" />
  </ItemGroup>

</Project>
3
  • To me, this behaves as intended. You shouldn't be allowed to post null-values unless you declare the property as nullable, right? However, I see how this could be a problem if you upgraded a large project and need to refactor hundreds of endpoints. Apologies for being slightly off-topic. Commented Mar 4, 2022 at 10:58
  • 2
    Is MyClass part of the WebAPI project? Commented Mar 4, 2022 at 11:09
  • Yes @ChristophLütjen , that was the answer! MyClass was part of another project, I disabled nullable there as well and now it works. Thanks! Commented Mar 4, 2022 at 11:26

5 Answers 5

25

This is a feature of C# 8, nullable reference types and in .NET 6 they are enabled by default. To turn it off, in your csproj file add this line <Nullable>disable</Nullable>. For example:

<PropertyGroup>
  <Nullable>disable</Nullable>
</PropertyGroup>

Also, if MyClass is in another project, add <Nullable>disable</Nullable> there as well.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help David but this doesn't help, I'm still getting 400 responses. I'll update the question.
I think this feature is more a compile-time feature, while the question seems to have a runtime problem after upgrading to asp core 6.
@Oliver - if nullable ref types are enabled, asp will respect this when verifying requests and return 400 if a string is required but no value or null is provided.
@ChristophLütjen: And learned something new today. 😉
I updated this answer based on @ChristophLütjen 's comments. This solved it for me.
6

If you disable nullable in your csproj, it may cause warnings when you use the nullable operator(?). Adding the following code to your .editorconfig will allow you to use both methods (nullable or without nullable) without any additional warnings.

You can edit your .editorconfig file in your solution project. Added these lines at the end of section [*.{cs,vb}]

#### Naming styles ####
[*.{cs,vb}]

# CS8618: Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
dotnet_diagnostic.CS8618.severity = none

# CS8601: Possible null reference assignment.
dotnet_diagnostic.CS8601.severity = none 

Comments

4

Just another approach, if you do use nullable variables/properties/parameters, you will receive another warning when setting the nullable to disable.

To prevent it, and also disable the nullable warnings, I recommend to use the option "ANOTATIONS", this will show warnings when you specify that something is nullable and will not asume it:

enter image description here

Comments

1

You can also enable or disable the checks differently per source file, e.g.

#nullable disable

See the C# preprocessor documentation for other options.

Comments

0

I had the same problem. I was posting null value from rest client to .NET core 6 API and the request was NULL. Added below line in program.cs and it worked.

builder.Services.AddControllers().AddNewtonsoftJson(options =>
{
    options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.