Posts tagged with “.netcore”

Fix swagger stuck on extend an API: How to configure Swashbuckle to ignore property on model

Referenced

I found the root cause is that the JSON generated by swagger is too large: Swagger outputs over 18000 lines for that API! Further research told me that two property fields contribute about 17000 lines. So how to configure swagger to ignore these two properties is the key. After googling a while, I found VeganHunter's answer in this thread Reference is the simplest.

Solution for .NET Core 3.1 and .NET Standard 2.1:

Use JsonIgnore from System.Text.Json.Serialization namespace.

( JsonIgnore from Newtonsoft.Json will NOT work )

public class Test
{
    [System.Text.Json.Serialization.JsonIgnore]
    public int HiddenProperty { get; set; }
    public int VisibleProperty { get; set; }
}

Hope it could also help someone else. 😀

.NET Core: Set the correct CurrentCulture basing the `lang` header

  1. Create a middleware to do that task

     public static void SetCurrentCulture(this IApplicationBuilder app)
     {
         app.Use(async (context, next) =>
         {
             var cultureQuery = context.Request.Headers["lang"] == "en" ? "en-US" : "zh-CN";
             if (!string.IsNullOrWhiteSpace(cultureQuery))
             {
                 var culture = new CultureInfo(cultureQuery);
                 CultureInfo.CurrentCulture = culture;
                 CultureInfo.CurrentUICulture = culture;
             }
             await next();
         });
     }
    
  2. Use the middleware in Startup.cs

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILog logger,
         IExceptionEntityService exceptionEntityService)
     {
         ...
         app.SetCurrentCulture();
         ...
     }
    

.NET Core 3.1 Swagger UI Tweaks

  1. Present enums as string. Modify your StartUp.cs, add the highlight part below:
             services.AddMemoryCache().AddMvcCore().AddJsonOptions(opts =>
                 {
                     opts.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
                 })
                 .AddDataAnnotationsLocalization();
    

  1. Enable XML Comments
    1. Manually add the two lines in PropertyGroup below to the .csproj file:
      <PropertyGroup>
        <GenerateDocumentationFile>true</GenerateDocumentationFile>
        <NoWarn>$(NoWarn);1591</NoWarn>
      </PropertyGroup>
      
    2. Add the options in the code below
           services.AddSwaggerGen(options =>
           {
               options.IncludeXmlComments(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{AppDomain.CurrentDomain.FriendlyName}.xml"), true);
           });
      

Samples for setting the urls and environment for a .net core application

bash

ASPNETCORE_URLS="http://*:9143" ASPNETCORE_ENVIRONMENT="Development" dotnet Yourproject.dll
ASPNETCORE_ENVIRONMENT="Development" dotnet run --urls "http://*:9143" --no-launch-profile

for Windows command line (cmd.exe)

setx ASPNETCORE_URLS "http://localhost:5001"
setx ASPNETCORE_ENVIRONMENT "Development"
dotnet Yourproject.dll

for Windows powershell

$Env: ASPNETCORE_URLS "http://localhost:5001"
$Env: ASPNETCORE_ENVIRONMENT "Development"
dotnet Yourproject.dll

Reference