ASP.NET Core 项目文件夹解读新框架

引言

庖丁解牛:“始臣之解牛之时,所见无非牛者;三年之后,未尝见全牛也。”

正文

首先贴出来项目文件夹的截图:
Core 文件夹

project.json 和global.jason

project.json是 .NET Core 项目中最重要的一个配置文件,类似于.NET Framework上的 .csproj文件。
首先,从我们 通过 Visual Studio 创建的项目 xproj 的 project.json︰

{
“version”: “1.0.0-*”,
“buildOptions”: {
“emitEntryPoint”: true
},

“dependencies”: {
“Microsoft.NETCore.App”: {
“type”: “platform”,
“version”: “1.0.0”
}
},

“frameworks”: {
“netcoreapp1.0”: {
“imports”: “dnxcore50”
}
}
}

dotnet new 命令创建项目的 project.json:

{
“version”: “1.0.0-*”,
“buildOptions”: {
“debugType”: “portable”,
“emitEntryPoint”: true
},
“dependencies”: {},
“frameworks”: {
“netcoreapp1.0”: {
“dependencies”: {
“Microsoft.NETCore.App”: {
“type”: “platform”,
“version”: “1.0.0”
}
},
“imports”: “dnxcore50”
}
}
}

1.1.2 Properties——launchSettings.json
启动配置文件,用于应用的启动准备工作,包括环境变量,开发端口等。

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

2.1.3Startup.cs
该文件是ASP.NET Core的启动入口文件(联想OWIN开发);项目运行时,编译器会在程序集中自动查找StartUp.cs文件读取启动配置。除了构造函数外,它可以定义Configure和ConfigureService方法。(英文版说明信息)
Program.cs Configures the host of the ASP.NET Core app.
Startup.cs Configures services and the request pipeline. See Startup.
(1)构造函数
用来启动配置文件

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) public void ConfigureServices(IServiceCollection services)
{

        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);
        services.AddMvc();
    }

(3)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?}");
            });
        }

2.1.4 wwwroot和bower.json
wwwroot 是一个存放静态文件的文件夹,存放了注入css,js,img等文件内容。说为什么.NET core开发灵活度提高,文件配置是手动配置,是其中一个原因。既然有存放静态文件的,那么也有存放文件引用的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 —— 管理你的客户端依赖关系

1.2.7 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)
                .AddEnvironmentVariables();

鱼尾

博主新近开始学习.NET Core ,本文主要参考博客文章:https://www.cnblogs.com/liangxiaofeng/p/5795239.html

猜你喜欢

转载自blog.csdn.net/qq_37040173/article/details/81454722