- ASP.NET Core 教程
- ASP.NET Core - 首頁
- ASP.NET Core - 概述
- ASP.NET Core - 環境設定
- ASP.NET Core - 新建專案
- ASP.NET Core - 專案佈局
- ASP.NET Core - Project.Json
- ASP.NET Core - 配置
- ASP.NET Core - 中介軟體
- ASP.NET Core - 異常處理
- ASP.NET Core - 靜態檔案
- ASP.NET Core - 設定 MVC
- ASP.NET Core - MVC 設計模式
- ASP.NET Core - 路由
- ASP.NET Core - 屬性路由
- ASP.NET Core - 操作結果
- ASP.NET Core - 檢視
- 設定 Entity Framework
- ASP.NET Core - DBContext
- ASP.NET Core - Razor 佈局檢視
- ASP.NET Core - Razor 檢視啟動
- ASP.NET Core - Razor 檢視匯入
- ASP.NET Core - Razor 標籤助手
- ASP.NET Core - Razor 編輯表單
- ASP.NET Core - 身份驗證概述
- ASP.NET Core - 授權屬性
- 身份驗證配置
- ASP.NET Core - 身份驗證遷移
- ASP.NET Core - 使用者註冊
- ASP.NET Core - 建立使用者
- ASP.NET Core - 登入和登出
- ASP.NET Core 有用資源
- ASP.NET Core - 快速指南
- ASP.NET Core - 有用資源
- ASP.NET Core - 討論
ASP.NET Core - 專案佈局
在本章中,我們將討論 ASP.NET Core 專案在檔案系統中的表現形式,以及不同的檔案和目錄是如何協同工作的。
讓我們開啟上一章建立的 **FirstAppDemo** 專案。
在解決方案資源管理器視窗中,右鍵單擊 **解決方案** 節點,然後選擇 **在檔案資源管理器中開啟資料夾**。
您現在將看到根目錄中包含兩個檔案:**FirstAppDemo.sln** 和 **global.json**。
**FirstAppDemo.sln** 是一個解決方案檔案。Visual Studio 多年來一直預設使用此副檔名,您可以雙擊該檔案以在 Studio 中開啟應用程式並進行操作。
還有一個 **global.json** 檔案。讓我們在 Visual Studio 中開啟此檔案。
在檔案中,專案的設定非常重要。此專案設定告訴 ASP.NET 在哪裡查詢您的原始碼以及哪些資料夾包含您的專案。
有兩個可能的資料夾:“**src**” 用於原始碼,以及一個“**test**” 資料夾。除非您的專案和原始碼位於這兩個資料夾之一中,否則程式碼將不可用於構建。您可以根據需要更改這些設定。
Windows 資源管理器中有磁碟上的“**src**” 資料夾。您沒有測試資料夾。在測試資料夾中,您可以放置您的單元測試專案。讓我們雙擊“src” 資料夾。
您可以看到 FirstAppDemo 專案和 Web 應用程式。現在,雙擊該資料夾。
這些是應用程式的原始碼檔案,您也可以在解決方案資源管理器視窗中看到此資料夾結構。這是因為在當前版本的 ASP.NET Core 中,檔案系統決定了專案中包含的內容。
如果將新檔案新增到磁碟,則該檔案將新增到專案中。如果刪除檔案,則該檔案將從專案中刪除。所有內容保持同步,這與以前版本的 ASP.NET Core 略有不同,在以前版本的 ASP.NET Core 中,專案檔案(一個 *.cs proj 檔案)包含專案中所有內容的清單。
ASP.NET Core 還會在檔案更改或出現新檔案時編譯您的應用程式。
示例
讓我們透過在文字編輯器中開啟 **Startup.cs** 檔案來看一個簡單的示例。
正是這行程式碼響應對應用程式的每個 HTTP 請求,並且它只是響應“Hello World!”。
讓我們更改上面截圖中的字串,改為“**Hello World! This ASP.NET Core Application**”,如下面的程式所示。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace FirstAppDemo {
public class Startup {
// This method gets called by the runtime.
// Use this method to add services to the container.
// For more information on how to configure your application,
// visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services) {
}
// This method gets called by the runtime.
// Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,
IHostingEnvironment env, ILoggerFactory loggerFactory) {
loggerFactory.AddConsole();
if (env.IsDevelopment()){
app.UseDeveloperExceptionPage();
}
app.Run(async (context) => {
await context.Response.WriteAsync(
"Hello World! This ASP.NET Core Application");
});
}
}
}
透過按 Ctrl + S 在文字編輯器中儲存此檔案,然後返回 Web 瀏覽器並重新整理應用程式。
您現在可以看到瀏覽器中反映了您的更改。
這是因為 ASP.NET 將監視檔案系統並在檔案更改時自動重新編譯應用程式。您無需在 Visual Studio 中顯式構建應用程式。
實際上,您可以使用完全不同的編輯器,例如 Visual Studio Code。
您只需使用 Visual Studio 啟動 Web 伺服器即可,無需偵錯程式。您也可以按 Ctrl + F5,並且可以編輯檔案、儲存檔案,只需重新整理瀏覽器即可檢視更改。
這是一種使用 C# 等編譯語言構建 Web 應用程式的不錯工作流程。