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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章