Net core学习系列(二)——Net Core项目文件简介

一、 ASP.NET Core 项目文件夹解读

ASP.NET Core 1.0 发布以来,相较于传统项目编码发布的行为,新项目中的操作已经有了很大的变化,如解析依赖,选择运行平台和Runtime等等,就连项目结构也有了比较大的改变,越来越多的配置选项由编辑器转交给了开发者手动决定,这一点在新的各类配置文件中体现得尤为明显,这里就来简单解读一下。

(一) 项目文件夹总览

(二) project.json和global.json

project.json是.NET Core项目中最重要的一个配置文件,它类似于.NET Framework上的 .csrpoj文件(在下一版本中.NET Core将弃用该文件,转而回归.csrpoj)。所以这里还是搬运下张大大的博客吧,包括对global.json的解读。 project.json 这葫芦里卖的什么药

(三) Properties——launchSettings.json

顾名思义——启动配置文件。launchSettings.json文件为一个ASP.NET Core应用保存特有的配置标准,用于应用的启动准备工作,包括环境变量,开发端口等。在launchSettings.json文件中进行配置修改,和开发者右键项目——属性中所提交的更改的效果是一样的(目前右键属性中的Property真是少得可怜),并且支持同步更新。

{
  "iisSettings": {                                 #选择以IIS Express启动  
    "windowsAuthentication": false,                #是否启用windows身份验证
    "anonymousAuthentication": true,               #是否启用匿名身份验证
    "iisExpress": {
      "applicationUrl": "http://localhost:24269/", #IIS Express随机端口
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },                                            
    "WebApplication": {                           #选择本地自宿主启动,详见Program.cs文件。删除该节点也将导致Visual Studio启动选项缺失
      "commandName": "Project",                   #
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",       #本地自宿主端口
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

(四)Startup.cs

Startup.cs文件是ASP.NET Core的启动入口文件,想必尝试过OWIN开发的一定不会陌生。项目运行时,编译器会在程序集中自动查找Startup.cs文件读取启动配置。除了构造函数外,它可以定义Configure和ConfigureServices方法。

1、构造函数

用来启动配置文件Configuration

  public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();

            if (env.IsDevelopment()) //读取环境变量是否为Development,在launchSettings.json中定义
            {
                // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
                builder.AddApplicationInsightsSettings(developerMode: true);
            }
            Configuration = builder.Build();
        }

2、ConfigureServices

ConfigureServices 用来配置我们应用程序中的各种服务,它通过参数获取一个IServiceCollection 实例并可选地返回 IServiceProvider。ConfigureServices 方法需要在 Configure 之前被调用。我们的Entity Framework服务,或是开发者自定义的依赖注入(ASP.NET Core自带的依赖注入也是无所不在),更多内容请见官方文档

  public void ConfigureServices(IServiceCollection services)
        {
            
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);
            services.AddMvc();
        }

3、 Configure

Configure 方法用于处理我们程序中的各种中间件,这些中间件决定了我们的应用程序将如何响应每一个 HTTP 请求。它必须接收一个IApplicationBuilder参数,我们可以手动补充IApplicationBuilder的Use扩展方法,将中间件加到Configure中,用于满足我们的需求。

  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();
            app.UseApplicationInsightsRequestTelemetry();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseApplicationInsightsExceptionTelemetry();

            app.UseStaticFiles(); 

            app.UseMvc(routes =>  //MVC路由配置
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

(五) bundleconfig.json

bundleconfig.json是一个压缩包的集合文件(这个不是很明白),这里有一篇bundleconfig.json specs,大意是它可以自动压缩关联文件用于项目中,如生成 <script><link>符号.

(六) wwwroot和bower.json

wwwroot是一个存放静态内容的文件夹,存放了诸如css,js,img等文件。刚才提到新的ASP.NET Core使开发灵活度大大提高,文件配置也都是手动为主,所以既然有存放文件的wwwroot,那也有存放文件引用的bower.json

{
  "name": "asp.net",
  "private": true,
  "dependencies": {
    "bootstrap": "3.3.6",
    "jquery": "2.2.0",
    "jquery-validation": "1.14.0",
    "jquery-validation-unobtrusive": "3.2.6"
  }
}

bower.json记录了项目需要的相关文件引用,我们可以在里面自由删除增加需要的文件,如jquery.form.js,Bower配置管理器也会自动帮我们在github上下载相关文件,下载后的文件也将放在wwwroot文件夹中。这些改变在项目的“依赖项”上都能直观查看。

Tips:每个项目中只能有一个bower.json配置文件,对于bower.json的详细信息请参见Bower —— 管理你的客户端依赖关系

(七)appsettings

同样是顾名思义——应用配置,类似于.NET Framework上的Web.Config文件,开发者可以将系统参数通过键值对的方式写在appsettings文件中(如程序的连接字符串),而Startup类中也在构造器中通过如下代码使得程序能够识别该文件

var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)

参考资料:https://www.cnblogs.com/liangxiaofeng/p/5795239.html

猜你喜欢

转载自www.cnblogs.com/wyh19941210/p/11460384.html