使用.net core开发web api手记

因平时工作太忙,在使用.net core开发项目时,遇到的一些知识点不明白或者在工作过程中自己解决了的问题做一个记录,本文是记录使用.net core开发的笔记,不成体系的文章,纯属记忆文章。

Fluent API配置方法

一对多的关系,在多的一段配置如下:

public class Blog

{

    public int BlogId { get; set; }

    public string Url { get; set; }

    public List<Post> Posts { get; set; }

}

public class Post

{

    public int PostId { get; set; }

    public string Title { get; set; }

    public string Content { get; set; }

    public int BlogForeignKey { get; set; }

    public Blog Blog { get; set; }

}

配置代码:

modelBuilder.Entity<Post>()

            .HasOne(p => p.Blog)

            .WithMany(b =>b.Posts)

            .HasForeignKey(p =>p.BlogForeignKey);

在asp.net core中提供了五种过滤器,分别用于实现不同的功能,它们分别是:Authorization Filter、Resource Filter、Action Filter、Exception Filter、Result Filter

Mysql查询挂起事务SELECT* FROM information_schema.INNODB_TRX

验证类型Dto类,枚举值不要给默认值,设置为Range验证即可。

.net core使用web services  步骤:

扩展更新:Microsoft WCF Web Service Reference Provider

基于.net core 的web api 开发手记

第一篇

使用.net core开发web api手记 1

Asp.net core 管道模型

JWT = 加密方法 ( Header + Payload )  //加密方法需要一个密钥

使用.net core开发web api手记 2

来源:http://blog.leapoahead.com/2015/09/06/understanding-jwt/

使用.net core开发web api手记 3
使用.net core开发web api手记 4

来源:https://my.oschina.net/qiaojieqq11/blog/676816

头部(Header)

{

 “typ”: “JWT”,

 “alg”: “HS256”

}

载荷(Payload)

iss:Issuer,发行者

sub:Subject,主题

aud:Audience,观众

exp:Expiration time,过期时间

nbf:Not before

iat:Issued at,发行时间

jti:JWT ID

{ “iss”: “Online JWTBuilder”,

 “iat”: 1416797419,

 “exp”: 1448333419,

 “aud”: “www.example.com”,

 “sub”: “[email protected]”,

 “GivenName”: “Johnny”,

 “Surname”: “Rocket”,

 “Email”: “[email protected]”,

 “Role”: [ “Manager”, “ProjectAdministrator” ]

}

签名(签名)

public enum HttpStatusCode

    {

       Continue = 100,

       SwitchingProtocols = 101,

       OK = 200,

       Created = 201,

       Accepted = 202,

       NonAuthoritativeInformation = 203,

       NoContent = 204,

       ResetContent = 205,

        PartialContent = 206,

       MultipleChoices = 300,

       Ambiguous = 300,

       MovedPermanently = 301,

       Moved = 301,

       Found = 302,

       Redirect = 302,

       SeeOther = 303,

       RedirectMethod = 303,

       NotModified = 304,

       UseProxy = 305,

       Unused = 306,

       TemporaryRedirect = 307,

       RedirectKeepVerb = 307,

       BadRequest = 400,

       Unauthorized = 401,

       PaymentRequired = 402,

       Forbidden = 403,

       NotFound = 404,

       MethodNotAllowed = 405,

       NotAcceptable = 406,

       ProxyAuthenticationRequired = 407,

       RequestTimeout = 408,

       Conflict = 409,

       Gone = 410,

       LengthRequired = 411,

       PreconditionFailed = 412,

       RequestEntityTooLarge = 413,

       RequestUriTooLong = 414,

       UnsupportedMediaType = 415,

       RequestedRangeNotSatisfiable = 416,

       ExpectationFailed = 417,

       InternalServerError = 500,

       NotImplemented = 501,

       BadGateway = 502,

       ServiceUnavailable = 503,

       GatewayTimeout = 504,

       HttpVersionNotSupported = 505

}

使用.net core开发web api手记 5

图片来源:http://www.cnblogs.com/xiekeli/p/5607107.html

使用.net core开发web api手记 6

图片来源:http://www.cnblogs.com/xiekeli/p/5607107.html

URL RewritingMiddleware in ASP.NET Core

more than onedbcontext was found. specify which one to use. use the ‘-context’ parameter forpowershell commands and the ‘–context’ parameter for dotnet commands.

.net core迁移时报上面的错的话,可能存在的情况是项目里面有两个上下文或者实现了IDesignTimeDbContextFactory接口的两个类

单元测试篇

断言         说明

Assert.Equal()  验证两个参数是否相等,支持字符串等常见类型。同时有泛型方法可用,当比较泛型类型对象时使用默认的IEqualityComparer<T>实现,也有重载支持传入IEqualityComparer<T>

Assert.NotEqual()    与上面的相反

Assert.Same()  验证两个对象是否同一实例,即判断引用类型对象是否同一引用

Assert.NotSame()    与上面的相反

Assert.Contains()      验证一个对象是否包含在序列中,验证一个字符串为另一个字符串的一部分

Assert.DoesNotContain()          与上面的相反

Assert.Matches()     验证字符串匹配给定的正则表达式

Assert.DoesNotMatch()  与上面的相反

Assert.StartsWith()  验证字符串以指定字符串开头。可以传入参数指定字符串比较方式

Assert.EndsWith()    验证字符串以指定字符串结尾

Assert.Empty() 验证集合为空

Assert.NotEmpty()   与上面的相反

Assert.Single() 验证集合只有一个元素

Assert.InRange()      验证值在一个范围之内,泛型方法,泛型类型需要实现IComparable<T>,或传入IComparer<T>

Assert.NotInRange()         与上面的相反

Assert.Null()     验证对象为空

Assert.NotNull()       与上面的相反

Assert.StrictEqual() 判断两个对象严格相等,使用默认的IEqualityComparer<T>对象

Assert.NotStrictEqual()    与上面相反

Assert.IsType()/Assert.IsType<T>()  验证对象是某个类型(不能是继承关系)

Assert.IsNotType()/

Assert.IsNotType<T>()

与上面的相反

Assert.IsAssignableFrom()/

Assert.IsAssignableFrom<T>()

验证某个对象是指定类型或指定类型的子类

Assert.Subset()         验证一个集合是另一个集合的子集

Assert.ProperSubset()      验证一个集合是另一个集合的真子集

Assert.ProperSuperset()  验证一个集合是另一个集合的真超集

Assert.Collection()    验证第一个参数集合中所有项都可以在第二个参数传入的Action<T>序列中相应位置的Action<T>上执行而不抛出异常。

Assert.All()       

验证第一个参数集合中的所有项都可以传入第二个Action<T>类型的参数而不抛出异常

。与Collection()类似,区别在于这里Action<T>只有一个而不是序列。

Assert.PropertyChanged()        验证执行第三个参数Action<T>使被测试INotifyPropertyChanged对象触发了PropertyChanged时间,且属性名为第二个参数传入的名称。

Assert.Throws()/Assert.Throws<T>()

Assert.ThrowsAsync()/

Assert.ThrowsAsync<T>()

验证测试代码抛出指定异常(不能是指定异常的子类)

如果测试代码返回Task,应该使用异步方法

Assert.ThrowsAny<T>()

Assert.ThrowsAnyAsync<T>()

验证测试代码抛出指定异常或指定异常的子类

如果测试代码返回Task,应该使用异步方法

猜你喜欢

本站最新优惠

Namesilo优惠:新用户省 $1 域名注册-优惠码:45D%UYTcxYuCloZ 国外最便宜域名!点击了解更多

特别优惠:免费赠送 $100 Vultr主机-限时优惠!英文站必备海外服务器!点击了解更多

VPS优惠:搬瓦工优惠码:BWH3OGRI2BMW 最高省5.83%打开外面世界的一款主机点击了解更多

加入电报群

【江湖人士】(jhrs.com)原创文章,作者:江小编,如若转载,请注明出处:https://jhrs.com/2018/26488.html

扫码加入电报群,让你获得国外网赚一手信息。

文章标题:使用.net core开发web api手记

(0)
江小编的头像江小编
上一篇 2018-11-12 13:04
下一篇 2019-01-03 16:49

热门推荐

发表回复

登录后才能评论
畅访海外网站,外贸/外企/科技工作者专用工具,无缝体验真实的互联网,解锁LinkedIn访问
$19.95 /年
直达官网