Asp.net Core学习笔记 1.cookie 和 Session 2.Filter 3.IOC 和 DI 使用 Swagger EF Core 启用延迟加载 WebApi传参困惑

之前记在github上的,现在搬运过来

变化还是很大的,感觉和Nodejs有点类似,比如中间件的使用 ,努力学习ing...

优点

  • 不依赖IIS
  • 开源和跨平台
  • 中间件支持
  • 性能优化
  • 无所不在的依赖注入
  • 标准日志记录
  • Mvc 和 WebApi 合二为一
  • MVC 标签帮助
  • CLI工具

因为《通用数据保护条例》(General Data Protection Regulation,简称GDPR)为欧洲联盟于2018年5月25日出台的条例, Asp.Net Core 2.1中已经应用了这个条例,需要用户同意才可以使用cookie和session
但是可以通过修改 Startup.cs中的 options.CheckConsentNeeded的返回值为false就可以使用了

services.Configure<CookiePolicyOptions>(options =>
 {
    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
    //  options.CheckConsentNeeded = context => true;
     options.CheckConsentNeeded = context => false;
     options.MinimumSameSitePolicy = SameSiteMode.None;
});

但是,规定出来了,还是要遵守的 可以通过一些复杂的设置来 达到同样的效果,可以查看文档(https://blog.csdn.net/canduecho/article/details/80651853)

Cookie 使用方式

//设置Cookie
Response.Cookies.Append("Name", "BOB", new CookieOptions { Expires = DateTime.Now.AddDays(3)});
//获取cookie
string name=HttpContext.Request.Cookies["Name"];
//删除Cookie
Response.Cookies.Delete("Name");

Session

需要修改Startup.cs 中ConfigureServices方法 添加session服务.

 public void ConfigureServices(IServiceCollection services)
 {
    services.Configure<CookiePolicyOptions>(options =>
    {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
     });

    services.AddSession();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
 }

然后在Configure方法中注册中间件 app.UseSession();

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();
    app.UseSession();
    app.UseMvc(routes =>
    {
        routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
         });
    }

读/写Session

//设置session
HttpContext.Session.SetString("User", JsonConvert.SerializeObject(new Models.User { Id = 1, Name = "Alice" }));
//获取session
Models.User user = JsonConvert.DeserializeObject<Models.User>(HttpContext.Session.GetString("User")) as Models.User;

2.Filter

在Startup.cs中修改中ConfigureServices方法,这样就是全局过滤器了,

全局过滤器配置

services.AddMvc(o =>
{
    new Filter.MyFilter();//自定义过滤器
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

具体的过滤器区别不大.. 还是和 Asp.Net Mvc 一样..这里只举例了一种ActionFilter

 public class MyFilter : Attribute,IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext context)
        {
            
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.HttpContext.Request.Cookies["Name"]==null)
            {
                context.HttpContext.Response.Redirect("http://www.baidu.com");
            }
        }
    }

3.IOC 和 DI

什么是IOC和DI可以看看这篇文章,我觉得讲的不错(https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html)

使用方式

    public class TestService: ITestService //这里不能漏哦,不然注入的时候转换失败...
    {
        public TestService()
        {
            MyProperty = Guid.NewGuid();
        }
        public Guid MyProperty { get; set; }
        public List<string> GetList(string a)
        {
            return new List<string>() { "LiLei", "ZhangSan", "LiSi" };
        }
    }

对应的接口

    public interface ITestService
    {
        Guid MyProperty { get; }
        List<string> GetList(string a);
    }

然后using Microsoft.Extensions.DependencyInjection;

    public void ConfigureServices(IServiceCollection services)
    {
        //这里就是注入服务
        services.AddTransient(typeof(ITestService), typeof(TestService));

        //如果是泛型类的话 需要这么写
        services.AddTransient(typeof(ITestService<>),typeof(TestService<>));

    }

AddTransient就是注入的方法之一,前面一个是你服务的接口,第二个是服务的实现类..

使用 Swagger

1.Nuget 安装 Swashbuckle.AspNetCore
2.在startup.cs中的 ConfigureServices 方法中增加

    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info
        {
            Version = "v1",
            Title = "StudentManager API",
            Description = "A simple example ASP.NET Core Web API"
        });
        //获取xml路径 加载xml
        var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)
        var xmlPath = Path.Combine(basePath, "StudentManager.WebApi.xml");
        c.IncludeXmlComments(xmlPath);
        //实体层xml
        c.IncludeXmlComments(Path.Combine(basePath, "StudentManager.Entity.xml"));
    });

3.在 Configure 方法中增加

    //启用中间件服务生成Swagger作为JSON终结点
    app.UseSwagger();
    //启用中间件服务对swagger-ui,指定Swagger JSON终结点
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "StudentManager Api");
        //设置根节点为SwaggerUI
        c.RoutePrefix = string.Empty;
    });

4.访问 http://localhost:/swagger 即可看到效果
5.更多设置查看(https://www.cnblogs.com/yilezhu/p/9241261.html) 或(https://www.cnblogs.com/yanbigfeg/p/9232844.html)

EF Core 启用延迟加载

  • 之前写博客api的时候总是出现导航属性取值为null的问题,用了特性标签关联数据都不行,后面找到原因了 是没有启用延迟加载,
  • 启用延迟加载,在EF Core 2.1中我们需要下载【Microsoft.EntityFrameworkCore.Proxies】包 在context文件中重写OnConfiguring方法 还有导航属性必须用virtual关键字修饰!! 具体详情[http://www.cnblogs.com/CreateMyself/p/9195131.html]
 protected override  void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 {
      optionsBuilder.UseLazyLoadingProxies(true);
 }
  • 启用了延迟加载 就会有循环引用的问题,我们需要在ConfigureServices方法中进行如下配置才行。
public void ConfigureServices(IServiceCollection services)
{
   services.AddMvc().AddJsonOptions(
   options => options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
}

WebApi传参困惑

前端如果是FromForm的话

 $.ajax({
  type: "POST",
  url: 'http://localhost:51543/api/values',
  data: {
        Num:5,
        Sum:10
  },
  success: function(data){
    console.log(data);
  },
  contentType: 'application/x-www-form-urlencoded'
});
[HttpPost]
public IActionResult Post([FromForm]NumNum n)
{
    return new JsonResult(n.Num);
}

如果是传递json的话

 $.ajax({
  type: "POST",
  url: 'http://localhost:51543/api/values',
  data: JSON.stringify({
        Num:5,
        Sum:10
  }),
  success: function(data){
    console.log(data);
  },
  contentType: 'application/json'
});
[HttpPost]
public IActionResult Post([FromBody]NumNum n)
{
  return new JsonResult(n.Num);
}