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 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的更多相关文章

  1. .net core 入坑经验 - 2、MVC Core之获取网站运行路径

    这次是建立了asp.net mvc core项目,在controller中想获取网站在硬盘中的路径,找了一圈Server.MapPath() 已不存在,HttpContent也一样,经过查阅资料发现是 ...

  2. .net core 入坑经验 - 3、MVC Core之jQuery不能使用了?

    在View中添加了一段jQuery代码用来控制一个按钮的点击事件.发现运行时提示$对象没有定义,经过在浏览器右键查看源文件发现,script代码在引用jquery代码的上方,执行时jquery还未引入 ...

  3. MVC Core

    .NET Core 1.1 发布 文档下载资源汇总 https://www.microsoft.com/net/core#windowsvs2015 https://docs.microsoft.co ...

  4. .NET Core 1.0、ASP.NET Core 1.0和EF Core 1.0简介

    .NET Core 1.0.ASP.NET Core 1.0和EF Core 1.0简介 英文原文:Reintroducing .NET Core 1.0, ASP.NET Core 1.0, and ...

  5. Professional C# 6 and .NET Core 1.0 - 40 ASP.NET Core

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 40 ASP.NET Core --- ...

  6. .NET Core 2.0和ASP.NET Core 2.0正式版抢先体验

    .NET Core 2.0和ASP.NET Core 2.0正式版抢先体验 .NET Standard 2.0 is final Broad platform support. .NET Standa ...

  7. ASP.NET Core 实战:使用 ASP.NET Core Web API 和 Vue.js 搭建前后端分离项目

    一.前言 这几年前端的发展速度就像坐上了火箭,各种的框架一个接一个的出现,需要学习的东西越来越多,分工也越来越细,作为一个 .NET Web 程序猿,多了解了解行业的发展,让自己扩展出新的技能树,对自 ...

  8. ASP.NET Core 实战:将 .NET Core 2.0 项目升级到 .NET Core 2.1

    一.前言  最近一两个星期,加班,然后回去后弄自己的博客,把自己的电脑从 Windows 10 改到 Ubuntu 18.10 又弄回 Windows 10,原本计划的学习 Vue 中生命周期的相关知 ...

  9. .NET CORE学习笔记系列(1)——ASP.NET MVC Core 介绍和项目解读

    ASP.NET MVC Core 项目文件夹解读 一.项目文件夹总览 1.1.Properties——launchSettings.json 启动配置文件,你可以在项目中“Properties”文件夹 ...

随机推荐

  1. 网页启动Windows服务

    如何在网页启动Windows服务   由于公司有许多windows服务进行业务的处理,所谓对服务的维护也是一个比较头痛的问题,因为自己也不知道服务什么时候自动停了,而且更主要的原因是服务都是由运维部门 ...

  2. [Usaco2008 Open] Clear And Present Danger 寻宝之路[最短路][水]

    Description     农夫约翰正驾驶一条小艇在牛勒比海上航行.     海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一 张藏宝图上说,如果他的路程上 ...

  3. [原]iOS Makefile Template

    export DEVELOPER_DIR := $(shell xcode-select --print-path) PLATFORM="$(DEVELOPER_DIR)/Platforms ...

  4. .NET面向对象特性之“继承”

    整体简介 1.理解继承——继承关系图 2.实现继承与接口多继承 3.new. virtual.override方法 4.抽象方法和抽象类的继承 5.继承的本质 6.继承的复用性.扩展性和安全性 7.多 ...

  5. 网络爬虫的C++程序

    [搜片神器]之DHT网络爬虫的C++程序初步开源 回应大家的要求,特地整理了一开始自己整合的代码,这样最简单,最直接的可以分析流程,至于文章里面提供的程序界面更多,需要大家自己开发. 谢谢园子朋友的支 ...

  6. .NET接口和类 反射的差异性发现

    1 背景 在项目中使用反射,反射出某类型的所有属性(Property)和对应的属性值.起初为了性能考虑在模块首次加载就反射类型的所有属性并将其存入字典.根据一般的编程规范——基于接口编程,所以首次传入 ...

  7. 大数据应用日志采集之Scribe演示实例完全解析

    大数据应用日志采集之Scribe演示实例完全解析 引子: Scribe是Facebook开源的日志收集系统,在Facebook内部已经得到大量的应用.它能够从各种日志源上收集日志,存储到一个中央存储系 ...

  8. C#基础原理拾遗——引用类型的值传递和引用传递

    C#基础原理拾遗——引用类型的值传递和引用传递 以前写博客不深动,只搭个架子,像做笔记,没有自己的思考,也没什么人来看.这个毛病得改,就从这一篇开始… 最近准备面试,深感基础之重要,奈何我不是计算机科 ...

  9. 【2013Esri全球用户大会精彩案例】Horry Count GIS--南卡罗来那州霍里县企业级应用

    行业领域:政府 基于Online的组织机构订阅服务,构建Horry县的地图及应用门户,满足政府和公众的需要.公众可通过移动设备查看公开的地图,包括教育资源分布.选区分布.墓地管理.害虫监控等内容:政府 ...

  10. Linux下逻辑卷创建与管理

    用虚拟机加一块硬盘后,查看硬盘状况,使用fdisk-l命令: [root@jerrybj ~]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes ...