zoukankan      html  css  js  c++  java
  • .NETCore_初探

    1. Core默认的服务注册

    Statup文件(rogram类型中创建 WebHost时使用的)中有俩个方法:
    Configure和ConfigureServices(将服务放置到容器里面)

    public void ConfigureServices(IServiceCollection services)
    {
    services.AddScoped<IService,Service>();
    services.AddTransient<IOperationTransient,Operation>();
    services.AddSingleton<IOperationSingleton,Operation>();
    }

    2. 使用第三方依赖注入容器

    .net Core 默认的容器只提供构造函数注入功能
    将默认容器替换成其他容器仅需三步:
    1. 将ConfigureServices方法的返回值改为IServiceProvider
    2. 将Asp.Net Core的服务注册到第三方容器中
    3. 使用第三方容器实现IServiceProvider接口并返回

    注:使用第三方容器必须将Controller注册为服务
    如果当容器变更为其它容器,并且使用了容器提供的如属性注入等功能时,如果没有将Controller注册为服务,那么相应的属性注入的过程也不会被触发,简单来说就是只有将Controller注册为服务,那么实例化Controller的工作才会由容器完成,才会触发或者使用到容器提供的其它特性。

    services.AddMvc()
    AddControllersAsServices()
    SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    3. 服务的获取
    参考:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.1
    1. Controller构造方法参数

    2. 通过Controller注入IServiceProvider类型,通过IServiceProvider来获取服务

    public HomeController(IServiceProvider serviceProvider)
    {
         var configuration = (IConfiguration)serviceProvider.GetServie(typeof(IConfiguration));
    }

    3. 在Action方法或者Mvc过滤器(过滤器的上下文参数中包含HttpContext)中通过HttpContext的RequestServices对象获取服务

    Public IActionResult Index()
    {
        var configuration = (IConfiguration)this.HttpContext.RequestServices.GetService(typeof(IConfiguration));
    }

    4. 在View使用@inject注入服务

    @using Microsoft.Extensions.Configuration;
    @inject IConfiguration configuration;

    5. 在Action方法中,通过FormServices特性注入服务

    public IActionResult Index([FromServices] IConfiguration configuration)
    {
        return this.View();
    }


    注:一般来说尽可能显式的标明类型的依赖(即通过构造参数的方式声明当前类型所依赖的组件)

    常用的服务

    1. IHostingEnvironment 包含环境名称,应用名称,当前程序根目录,以及wwwroot的根目录
    2. IHttpContextAccessor 当前请求的HttpContext
    3. IConfiguration 配置信息对象(不建议使用这个对象读取配置文件,使用Options)
    4. IServiceProvider 服务提高器
    5. DbContext EFCore的DbContext也是在ConfigureServices中进行服务注入(避免与Controller直接产生依赖关系)

    Configure方法之.NET Core请求管道的建立

    .NET Core基于Startup类型的Configure方法建立管道,通过IApplicationBulider实例添加不同功能的中间件,通过中间件的串联形成管道
    参考:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/index?view=aspnetcore-2.1

    public void Configure(IApplicationBuilder app,IHostingEnvironment env)
    {
    if(env.IsDevelopment())
    {//开发环境显示异常信息中间件
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }else
    {//其他环境跳转错误页面中间件
        app.UseExceptionHandler("/Error");
    }



    //默认的无参UseStaticFiles方法将wwwroot目录作为静态资源存放目录,静态文件处理中间件
    app.UseStaticFiles();
    //如果要添加其它静态内容目录可以再次使用UseStaticFiles方法,并通过StaticFileOptions对目录的访问路径以及实际路径进行配置

    app.UseStaticFiles(new StaticFileOptions{
        FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(),"Core")),
        RequestPath ="/Core"
    });


    //注:Windows和Linux类系统的路径分隔符也不一致,所以为了保证路径的统一,可以使用Path.Combine方法,该方法会根据操作系统的不同对路径进行不同的处理

    app.UseMvc(rotes =>
    {//Mvc中间件
        rotes.MapRoute(
        name: "default",
        template: "{controller}/{action=Index}/{id?}"
    );
    });
    }

    .NET Core Mvc
    1. 路由 将请求的Url映射到路由模板进行匹配,例如:
    //携带Area的路由模板必须放在前面,否则产生的地址则是:/Controller/Action?Area=area ASP.NET Core会将多余的路由参数放置到查询字符串中

    rotes.MapRoute(
    name: "areas",
    template: "{area:exists}/{controller = Home}/{action=Index}/{id?}"
    );
    rotes.MapRoute(
    name: "default",
    template: "{controller = Home}/{action=Index}/{id?}"
    );
    routes.MapRoute(
    name: "us_default",
    template: "en-us/Home/{id}",
        deatults: new { controller = "Home", action ="Index" },// 路由默认值
        constraints: new { id = new IntRouteConstraint() }, //路由参数约束
        dataTokens: new { locale ="en-us" } //附加数据
    );

    2. 路由除了处理Url请求匹配,还可以链接生成的功能,在View使用
    参考:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-2.1
    <a asp-area="" asp-controller="Home" asp-action="Index">Home</a>

    3. Area下面的Controller需要使用特性标记当前的Controller属于哪个Area;
    [Area("SysManger")]
    public class HomeController : Controller
    {
    ...

    4. View
    Razor参考:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-2.1
    TagHelper参考:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro?view=aspnetcore-2.1

    Core可以在ConfigureServices方法中对RazorViewEngieOptions进行配置
    services.Configure<RazorViewEngineOptions>(options =>
    {
    //添加View的查找路径
    options.ViewLocationFormats.Add("/Common/View/{1}/{0}.cshtml");
    //添加AreaView的查找路径
    options.AreaViewLocationFormats.Add("/Test/{2}/View/{1}/{0}.cshtml");
    });

    5. Action的返回值与Json序列化
    使用Json方法返回一个对象实例,使用首字母大写(WebApi的OK方法和Signalr的Json格式相同问题)
    public IActionResult GetObj()
    {
    return Json(new { code=0, Msg ="1"})
    }
    要使用"Msg"首字母大写命名需要配置Mvc服务添加以下代码修改Json默认的序列号配置:
    services.AddMvc()
    .AddJsonOptions(options =>
    {
    options.SerializerSettings.ContractResolver = new DefaultContractResolver();
    });

  • 相关阅读:
    LeetCode 189. Rotate Array
    LeetCode 965. Univalued Binary Tree
    LeetCode 111. Minimum Depth of Binary Tree
    LeetCode 104. Maximum Depth of Binary Tree
    Windows下MySQL的安装与配置
    LeetCode 58. Length of Last Word
    LeetCode 41. First Missing Positive
    LeetCode 283. Move Zeroes
    《蚂蚁金服11.11:支付宝和蚂蚁花呗的技术架构及实践》读后感
    删除docker下的镜像
  • 原文地址:https://www.cnblogs.com/ingstyle/p/10108368.html
Copyright © 2011-2022 走看看