C
C#3mo ago
Fruity Mike

lib for asp.net core dependency issues

Hey I want to move out and generalize certain api configurations, middlewares, etc. One of these would be json options:
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.AllowInputFormatterExceptionMessages = true; options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
})
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.AllowInputFormatterExceptionMessages = true; options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
})
I have written extension method for it:
public static IMvcBuilder AddDefaultJsonOptions(this IMvcBuilder builder, Action<JsonOptions> configure)
{
builder.AddJsonOptions(options =>
{ options.AllowInputFormatterExceptionMessages = true; options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
configure.Invoke(options);
});
return builder;
}
public static IMvcBuilder AddDefaultJsonOptions(this IMvcBuilder builder, Action<JsonOptions> configure)
{
builder.AddJsonOptions(options =>
{ options.AllowInputFormatterExceptionMessages = true; options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
configure.Invoke(options);
});
return builder;
}
Should be working, but 'JsonOptions' definition is missing and I'm unable to figure out/find necessary nuget. So far I'm using these packages:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
</ItemGroup>
Any tips would be appreciated. P.S. using .net 8
4 Replies
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Fruity Mike
Fruity MikeOP3mo ago
hmmm so you're saying that I shouldnt configure it exclusively for controllers?
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Fruity Mike
Fruity MikeOP3mo ago
okay but still the thing is that in seperate lib, where I want to move out those configurations, some sort of ref/nuget is missing so 'JsonOptions' definition is not available I'm aware now that it can be in done multiple ways. did more googling with different keywords and found the solution still should refactor things to use minimal apis instead of controllers what I can do is add this:
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
and all asp.net core definitions are going to be available
Want results from more Discord servers?
Add your server