Net core Learning Series (two) - Introduction Net Core project file

A, ASP.NET Core project folder interpretation

ASP.NET Core since the 1.0 release, as compared to the behavior of traditional project coding released, the operation of the new project already has changed a lot, such as analytical dependence, selected operating platform and Runtime and so on, and even the structure of the project has also been relatively a big change, a growing number of configuration options for transfer by the editor to the developers decided manually, which is in all kinds of new profile was especially evident, simply reading about it here.

(A) an overview of the project folder

(B) project.json and global.json

project.json .NET Core is the most important project in a configuration file, which is similar .csrpoj files on the .NET Framework (in the next version of .NET Core will abandon the file, turn back .csrpoj). So here still carry big blog it under the sheets, including the interpretation of global.json. project.json what this snake oil medicine

(三) Properties——launchSettings.json

As the name suggests - the startup configuration file. launchSettings.jsonSave the file to a specific configuration standard ASP.NET Core application, the application used to start the preparatory work, including environmental variables, such as the development of the port. In the launchSettings.jsonfiles of configuration changes, and developers right project - in effect property changes submitted is the same (currently right properties Property is woefully inadequate), and supports synchronized.

{
  "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

Guess you like

Origin www.cnblogs.com/wyh19941210/p/11460384.html