Posts tagged with “.netcore”

[Solution]Microsoft.AspNetCore.Http was marked deprecated

Remove those deprecated NuGet packages, use the ASP.NET Core shared framework instead

With the release of .NET Core 3.0, many ASP.NET Core assemblies are no longer published to NuGet as packages. Instead, the assemblies are included in the Microsoft.AspNetCore.App shared framework, which is installed with the .NET Core SDK and runtime installers. For a list of packages no longer being published, see Remove obsolete package references.

As of .NET Core 3.0, projects using the Microsoft.NET.Sdk.Web MSBuild SDK implicitly reference the shared framework. Projects using the Microsoft.NET.Sdk or Microsoft.NET.Sdk.Razor SDK must reference ASP.NET Core to use ASP.NET Core APIs in the shared framework.

To reference ASP.NET Core, add the following element to your project file:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

</Project>

Reference

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