標籤:man from list 檔案夾 home project mod use mapr
1.ASP.NET Core the Unified FrameworkASP.NET Core的統一架構
2.
New Solution
Project新的解決方案項目
- src folder: contains all projects that contain source code that make up your application.
- Program.cs: this file contains the Main method of an ASP.NET Core RC2 app, which is responsible for configuring and running the app.
- global.json: this is where you put solution-level settings and allows you to do project-to-project references.
- wwwroot: is a folder in which all your static files will be placed. These are the assets that the web app will serve directly to the clients, including HTML, CSS, Image and JavaScript files.
- project.json: contains project settings.
- Startup.cs: this is where you put your startup and configuration code.
- References: it contains the .NETCoreApp Version 1 runtime references.
3.
Configure the Application Pipeline
配置應用程式管道
public void Configure(IApplicationBuilder app){ app.UseDeveloperExceptionPage(); app.UseMvc(m => { m.MapRoute( name: "default", template: "{controller}/{action}/{id?}", defaults: new { controller = "Home", action="Index"}); });}View Code
串連mvc各個組件,添加相關依賴項
public void ConfigureServices(IServiceCollection services){ services.AddMvc();}View Code 4.
Introducing Inject
新特性Inject
a.定義類或服務
using System.Linq;using System.Threading.Tasks;namespace MVCCoreDemo.Models{ public class HeroStats { private HeroManager _manager = new HeroManager(); public async Task<int> GetHeroCount() { return await Task.FromResult(_manager.GetAll.Count()); } public async Task<int> GetHeroCountByType(string type) { return await Task.FromResult(_manager.GetHeroesByType(type).Count); } }}View Code
b.在視圖中使用
@model IEnumerable<MVCCoreDemo.Models.DOTAHero>@inject MVCCoreDemo.Models.HeroStats Stats<h3>My Favorite DOTA 2 Heroes</h3><ul> @foreach (var p in Model) { <li>@($"{p.Name} {p.Type}")</li> }</ul><div> <h4>Stats</h4> <p>Number of Strength Heroes: @await Stats.GetHeroCountByType("strength")</p> <p>Number of Agility Heroes: @await Stats.GetHeroCountByType("agility")</p> <p>Number of Intelligence Heroes: @await Stats.GetHeroCountByType("intelligence")</p> <p>Total Heroes Heroes: @await Stats.GetHeroCount()</p></div>View Code
c.依賴注入類或服務
public void ConfigureServices(IServiceCollection services){ services.AddMvc(); services.AddTransient<MVCCoreDemo.Models.HeroStats>();}View Code
5.
Introducing View Components
新特性View Components
遵循ViewComponent結尾約定
using System.Collections.Generic;using System.Threading.Tasks;using Microsoft.AspNetCore.Mvc;using MVCCoreDemo.Models;namespace MVCCoreDemo.ViewComponents{ public class HeroListViewComponent: ViewComponent { public async Task<IViewComponentResult> InvokeAsync(string type){ var heroes = await GetHeroesAsync(type); return View(heroes); } private Task<IEnumerable<DOTAHero>> GetHeroesAsync(string type){ return Task.FromResult(GetHeroes(type)); } private IEnumerable<DOTAHero> GetHeroes(string type){ HeroManager HM = new HeroManager(); return HM.GetHeroesByType(type); } }}View Code
檔案夾建立遵循Components檔案夾,子檔案夾命名遵循去除ViewCompont
視圖預設Default.cshtml
@model IEnumerable<MVCCoreDemo.Models.DOTAHero><h3>@Model.First().Type Heroes</h3><ul> @foreach (var p in Model) { <li>@p.Name</li> }</ul>View Code
調用ViewComponent
<div> @await Component.InvokeAsync("HeroList", new { type = "agility" })</div>View Code
原文連結:https://www.codeproject.com/articles/1104729/asp-net-core-getting-started-with-asp-net-mvc-core?msg=5258574
ASP.NET Core: Getting Started with ASP.NET MVC Core(一)