diff --git "a/\347\216\213\345\251\211\345\251\267/20241111-\345\210\233\345\273\272MVC\346\226\207\344\273\266.md" "b/\347\216\213\345\251\211\345\251\267/20241111-\345\210\233\345\273\272MVC\346\226\207\344\273\266.md" new file mode 100644 index 0000000000000000000000000000000000000000..67a94d995c011f5d88c0b9ea7667e33286ae2854 --- /dev/null +++ "b/\347\216\213\345\251\211\345\251\267/20241111-\345\210\233\345\273\272MVC\346\226\207\344\273\266.md" @@ -0,0 +1,310 @@ +在Visual Studio Code (VSCode) 中使用命令行界面(CMD)创建ASP.NET MVC项目的文件 + +```markdown +# ASP.NET MVC 文件创建指南(使用 VSCode 和 CMD) + +## 环境准备 +确保你已经安装了以下工具: +- Visual Studio Code (VSCode) +- .NET Core SDK +- Git Bash 或 CMD + +## 创建项目 +首先,在命令行界面中创建一个新的ASP.NET MVC项目。 + +```bash +# 使用 .NET CLI 创建一个新的 MVC 项目 +dotnet new mvc -n MyMVCApp +cd MyMVCApp +``` + +## 控制器(Controllers) +控制器处理用户请求并返回视图或数据。 + +### 创建控制器 +```bash +# 创建一个新的控制器 +dotnet new mvc -o Controllers/Home +``` + +### 控制器示例代码 +```csharp +// 在 Controllers 文件夹中创建一个新的控制器文件 Home.cs +using Microsoft.AspNetCore.Mvc; + +namespace MyMVCApp.Controllers +{ + public class HomeController : Controller + { + public IActionResult Index() + { + return View(); + } + } +} +``` + +## 视图(Views) +视图负责显示数据,通常使用Razor语法。 + +### 创建视图 +```bash +# 创建一个新的视图文件 +mkdir -p Views/Home +code Views/Home/Index.cshtml +``` + +### 视图示例代码 +```html +@{ + ViewData["Title"] = "Home Page"; +} + +

Welcome to the Home Page

+``` + +## 模型(Models) +模型代表应用程序的数据结构。 + +### 创建模型 +```bash +# 创建一个新的模型文件 +mkdir -p Models +code Models/Product.cs +``` + +### 模型示例代码 +```csharp +namespace MyMVCApp.Models +{ + public class Product + { + public int Id { get; set; } + public string Name { get; set; } + public decimal Price { get; set; } + } +} +``` + +## 辅助方法(Helpers) +辅助方法用于在视图中生成HTML。 + +### 创建辅助方法 +```bash +# 创建一个新的辅助方法文件 +mkdir -p Helpers +code Helpers/HtmlExtensions.cs +``` + +### 辅助方法示例代码 +```csharp +using Microsoft.AspNetCore.Html; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Microsoft.AspNetCore.Razor.TagHelpers; + +namespace MyMVCApp.Helpers +{ + public static class HtmlExtensions + { + public static IHtmlContent MyCustomHelper(this IHtmlHelper htmlHelper, string message) + { + return new HtmlString($"
{message}
"); + } + } +} +``` + +## 过滤器(Filters) +过滤器用于在执行控制器动作之前或之后执行代码。 + +### 创建过滤器 +```bash +# 创建一个新的过滤器文件 +mkdir -p Filters +code Filters/MyActionFilter.cs +``` + +### 过滤器示例代码 +```csharp +using Microsoft.AspNetCore.Mvc.Filters; + +namespace MyMVCApp.Filters +{ + public class MyActionFilter : IActionFilter + { + public void OnActionExecuting(ActionExecutingContext context) + { + // 执行前的操作 + } + + public void OnActionExecuted(ActionExecutedContext context) + { + // 执行后的操作 + } + } +} +``` + +## 路由(Routing) +路由配置通常在 `Startup.cs` 文件中设置。 + +### 配置路由 +```csharp +// 在 Startup.cs 文件中配置路由 +public void ConfigureServices(IServiceCollection services) +{ + services.AddControllersWithViews(); +} + +public void Configure(IApplicationBuilder app, IWebHostEnvironment env) +{ + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + app.UseExceptionHandler("/Home/Error"); + app.UseHsts(); + } + + app.UseHttpsRedirection(); + app.UseStaticFiles(); + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute( + name: "default", + pattern: "{controller=Home}/{action=Index}/{id?}"); + }); +} +``` + +## 布局(Layouts) +布局定义了视图的通用结构。 + +### 创建布局 +```bash +# 创建一个新的布局文件 +mkdir -p Views/Shared +code Views/Shared/_Layout.cshtml +``` + +### 布局示例代码 +```html + + + + + @ViewData["Title"] - My MVC App + + +
+ +
+
+ @RenderBody() +
+ + + +``` + +## 部分视图(Partial Views) +部分视图是视图的一部分,可以被多个视图重用。 + +### 创建部分视图 +```bash +# 创建一个新的部分视图文件 +mkdir -p Views/Shared +code Views/Shared/_MyPartialView.cshtml +``` + +### 部分视图示例代码 +```html +@model MyMVCApp.Models.Product + +
+

@Model.Name

+

@Model.Price

+
+``` + +## 视图组件(View Components) +视图组件是可重用的视图渲染逻辑。 + +### 创建视图组件 +```bash +# 创建一个新的视图组件文件 +mkdir -p ViewComponents +code ViewComponents/MyViewComponent.cs +``` + +### 视图组件示例代码 +```csharp +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ViewComponents; + +namespace MyMVCApp.ViewComponents +{ + public class MyViewComponent : ViewComponent + { + public IViewComponentResult Invoke() + { + var model = new Product { Name = "Sample Product", Price = 19.99m }; + return View(model); + } + } +} +``` + +## 服务(Services) +服务通常用于处理业务逻辑。 + +### 创建服务 +```bash +# 创建一个新的服务文件 +mkdir -p Services +code Services/IProductService.cs +``` + +### 服务接口示例代码 +```csharp +namespace MyMVCApp.Services +{ + public interface IProductService + { + IEnumerable GetProducts(); + } +} +``` + +### 服务实现示例代码 +```csharp +using MyMVCApp.Models; +using System.Collections.Generic; + +namespace MyMVCApp.Services +{ + public class ProductService : IProductService + { + public IEnumerable GetProducts() + { + return new List + { + new Product { Id = 1, Name = "Product 1", Price = 9.99m }, + new Product { Id = 2, Name = "Product 2", Price = 19.99m } + }; + } + } +} + +