SoFunction
Updated on 2025-03-04

.NET Core 2.2 new features are available in the newest

Preface

On December 4, local time in the United States, a series of major news was released at Microsoft's 2019 Developer Conference, including various good news from the software and hardware and open source community. As an ordinary developer, I downloaded the source code of .NET Core 2.2 as soon as possible, browsed the release instructions one by one, and intercepted some commonly used functions to try. I will share it with you below. Without further ado, let's take a look at the detailed introduction

1. Support for unified case of API interfaces

1.1 View the following interface code

  [HttpGet]
  public ActionResult<UserInfo> Get()
  {
   return new UserInfo() { Name = "", RegTime =  };
  }

  [HttpGet("{id}")]
  public ActionResult<Dictionary<string, string>> Get(int id)
  {
   return new Dictionary<string, string> {
    { "Name", "" },
    { "RegTime", () }
   };
  }

  // Interface 1 output  {
   name: "",
   regTime: "2018-12-05T10:40:37.5090634+08:00"
  }
  // Interface 2 output  {
   Name: "",
   RegTime: "2018-12-05T10:40:58.5072645+08:00"
  }

1.2 By default, the dictionary inland field names will not be applied CamelCaseNamingStrategy , so if you want to keep the field names in a unified case, you can add AddJsonOptions(o => (true)) in ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
().AddJsonOptions(o => (false)).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

AddJsonOptions has two built-in default extensions. You can use UseCamelCasing or UseMemberCasing. If UseMemberCasing is used, it means that the case rules of member fields are used, that is, the output is not changed.

1.3 Interestingly, the way AddJsonOptions(o => (true)) explicitly passes in the value is the result of this guy JamesNK. See for details

/aspnet/Mvc/pull/7962

2. Composite verification-Extension of verification model

1.1 In previous versions, if you want to apply multiple verifications to a property, you must write multiple verification classes, such as

public class UserInfo
{
[StringLength(20), RegularExpression(@"^[a-zA-Z]$")]
public string Name { get; set; }
[StringLength(20), RegularExpression(@"^[a-zA-Z]$")]
public string Title { get; set; }
public DateTime RegTime { get; set; }
}

2.2 In versions after .NET Core 2.2, you can avoid this problem by extending it, and implement composite verification by inheriting from ValidationProviderAttribute and rewriting the GetValidationAttributes method.

public class UserInfo
{
[Name]
public string Name { get; set; }
[Name]
public string Title { get; set; }
public DateTime RegTime { get; set; }
}

public class NameAttribute : ValidationProviderAttribute
{
public override IEnumerable<ValidationAttribute> GetValidationAttributes()
{
return new List<ValidationAttribute>
{
new RequiredAttribute(),
new RegularExpressionAttribute(pattern: "[A-Za-z]*"),
new StringLengthAttribute(maximumLength: 20)
};
}
}

2.3 Does it look much more concise?

3. API Controller adds the default response processing type

3.1 In previous versions, we can handle different HttpCode responses by adding the feature ProducesResponseType on the API, and then pranavkm thinks that we should add a default response processing type like Swagger/OpenApi, and then it appears.

namespace 
{
/// <summary>
/// A filter that specifies the type of the value and status code returned by the action.
/// </summary>
[AttributeUsage( | , AllowMultiple = true, Inherited = true)]
public class ProducesResponseTypeAttribute : Attribute, IApiResponseMetadataProvider
{
....

}

3.2 To be honest, I don’t understand how to use the above class. If you know, please reply in the comments. I will add it to the article. Thank you.

4. Razor view partial optimization

4.1 The .NET Core team believes that in the Razor view, if you use @ to introduce a branch view, there may be a potential deadlock, so change @ to

//Old:@("_StatusMessage", )

// New:&lt;partial name="_StatusMessage", for="StatusMessage" /&gt;

4.2 If you try to create a new MVC project using .NET Core 2.2 now, you will see the change immediately

5. Hook

5.1 By setting environment variables, you can execute some business logic before the program Main method is run, but the .NET Core team recommends that this function is just a low-level hook and should not be used for complex businesses. If necessary, you should still use dependency injection. Try this function again when you have time. It should be interesting.

Conclusion

In the .NET Core 2.2 version, there are many performance optimizations. It can be seen that the power of the open source community is indeed powerful. This article only excerpts some common functions for trying. I believe that more friends will share them in the future.

Looking forward to the arrival of 3.0 soon

Okay, the above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.