首页 > 代码库 > ASP.NET Core: Getting Started with ASP.NET MVC Core(一)
ASP.NET Core: Getting Started with ASP.NET MVC Core(一)
1.
ASP.NET Core the Unified Framework
ASP.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"}); }); }
连接mvc各个组件,添加相关依赖项
public void ConfigureServices(IServiceCollection services){ services.AddMvc(); }
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); } } }
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>
c.依赖注入类或服务
public void ConfigureServices(IServiceCollection services){ services.AddMvc(); services.AddTransient<MVCCoreDemo.Models.HeroStats>(); }
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); } } }
文件夹创建遵循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>
调用ViewComponent
<div> @await Component.InvokeAsync("HeroList", new { type = "agility" }) </div>
原文链接: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(一)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。