当前位置: 移动技术网 > IT编程>开发语言>.net > .NET Core 2.2 新增部分功能使用尝鲜

.NET Core 2.2 新增部分功能使用尝鲜

2018年12月06日  | 移动技术网IT编程  | 我要评论

前言

    美国当地时间12月4日,微软2019开发者大会中发布了一系列的重磅消息,包含了软硬件和开源社区的各种好消息是铺天盖地,作为一名普通的开发者,我第一时间下载了 .net core 2.2 的源码,针对发布说明逐条浏览,并截取了部分常用的功能进行尝试,下面就与大家分享。

1. 对 api 接口统一大小写的支持
  • 1.1 查看以下接口代码
       [httpget]
        public actionresult<userinfo> get()
        {
            return new userinfo() { name = "ron.liang", regtime = datetime.now };
        }

        [httpget("{id}")]
        public actionresult<dictionary<string, string>> get(int id)
        {
            return new dictionary<string, string> {
                { "name", "ron.liang" },
                { "regtime", datetime.now.tostring() }
            };
        }

        // 接口 1 输出
        {
            name: "ron.liang",
            regtime: "2018-12-05t10:40:37.5090634+08:00"
        }
        // 接口 2 输出
        {
            name: "ron.liang",
            regtime: "2018-12-05t10:40:58.5072645+08:00"
        }
  • 1.2 默认情况下,字典内地字段名称将不会被应用 camelcasenamingstrategy ,所以如果要保持字段名称大小写统一的问题,可在 configureservices 中加入 addjsonoptions(o => o.usecamelcasing(true))
   public void configureservices(iservicecollection services)
        {
            services.addmvc().addjsonoptions(o => o.usecamelcasing(false)).setcompatibilityversion(compatibilityversion.version_2_2);
        }
  • addjsonoptions 内置两个默认扩展,你可以使用 usecamelcasing 或者 usemembercasing ,如果使用 usemembercasing ,表示使用成员字段的大小写规则,即不改变大小写输出

  • 1.3 有意思的是,addjsonoptions(o => o.usecamelcasing(true)) 显式传入值的方式是由 jamesnk 这个哥们杠出来的结果,详见

https://github.com/aspnet/mvc/pull/7962
2. 复合验证-验证模型的扩展
  • 1.1 在之前的版本中,如果希望对一个属性应用多个验证,必须书写多个验证类,如
    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 在 .net core 2.2 以后的版本中,你可以通过扩展来避免这个问题,通过继承自 validationproviderattribute 并重写 getvalidationattributes 方法来实现复合验证
    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 看起来是不是简洁多了
3. api controller 增加默认的响应处理类型
  • 3.1 在以前的版本中,可以通过在 api 上增加特性 producesresponsetype 来处理不同的 httpcode 响应,然后 pranavkm 觉得,我们应该像 swagger/openapi 一样,增加一个默认的响应处理类型,然后就出现了
namespace microsoft.aspnetcore.mvc
{
    /// <summary>
    /// a filter that specifies the type of the value and status code returned by the action.
    /// </summary>
    [attributeusage(attributetargets.class | attributetargets.method, allowmultiple = true, inherited = true)]
    public class producesresponsetypeattribute : attribute, iapiresponsemetadataprovider
    {
        ....

    }
  • 3.2 说实话,上面的这个类,我没搞懂到底怎么用,有知道的朋友请在评论中回复,我将把它加入文中,感谢。

4. razor 视图部分优化

  • 4.1 .net core 团队认为,在 razor 视图中,如果使用 @html.parital 引入分部视图,可能存在潜在的死锁情况,所以将 @html.parital 变更为
 //旧的:
 @html.partial("_statusmessage", model.statusmessage)

 // 新的:
 <partial name="_statusmessage", for="statusmessage" />
  • 4.2 如果你现在尝试使用 .net core 2.2 创建新的 mvc 项目,你就马上可以看到该变化了
5. 钩子
  • 5.1 通过设置环境变量,可以在程序 main 方法运行前执行一些业务逻辑,但是 .net core 团队建议,该功能只是一些低级的钩子,不要用于复杂的业务,如有需要,还是应该使用依赖注入,有空再尝试一下该功能,应该会很有意思

结语

  • 在 .net core 2.2 版本中,有很多性能上的优化,可以看到开源社区的力量确实强大,本文仅节选了部分常用功能进行尝试,相信后续会有更多朋友的分享
  • 期待 3.0 早日到来

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网