Notatki dla .NET w języku programowania C#

Json.Newton Setting

$type as property with namespace

services.AddControllers().AddNewtonsoftJson(
           x => x.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All
);

Swagger Polymorphism

 services.AddSwaggerGen(c =>
            {
                //c.UseOneOfForPolymorphism();
                c.UseAllOfForInheritance();
                c.SelectDiscriminatorNameUsing(_ => "discriminator");
                //c.CustomSchemaIds(type => type.ToString());

Swagger own Schema Proccesor

app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "*****.**** API V1");
                //c.GeneratorSettings.SchemaProcessors.Add(new InheritanceSchemaProcessor());
            });
 
 
            // app.UseSwaggerUI(typeof(Startup).GetTypeInfo().Assembly, settings =>
            // {
            //     settings.GeneratorSettings.SchemaProcessors.Add(new InheritanceSchemaProcessor());
            // });
Kategoria: 

revert Migration dotnet EntityFramework

To revert the last applied migration you should (package manager console commands):

Revert migration from database: PM> Update-Database
Remove migration file from project (or it will be reapplied again on next step)
Update model snapshot: PM> Remove-Migration

Kategoria: 

Razor - model - array - js

Razor - model - array - js

 var courierList = @Html.Raw(Json.Serialize(@Model.ToDictionary(x => x.CarrierId, x => x.CarrierName)));
 
<./code>

object

                return Json(new[] {
                    new { description = "blabla" }
                });
Kategoria: 

core 3 - Json UpperCase

Core 3 uses System.Text.Json, which by default does not preserve the case. As mentioned with this GitHub issue, setting the PropertyNamingPolicy to null will fix the problem.

public void ConfigureServices(IServiceCollection services)
{
...
services.AddControllers()
.AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = null);

and if you don't want to change the global settings, for one action only it's like this:

return Json(obj, new JsonSerializerOptions { PropertyNamingPolicy = null });

Kategoria: 

Strony

Subskrybuj Notatki dla .NET w języku programowania C#