码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET Core: Getting Started with ASP.NET MVC Core(一)

时间:2017-01-13 22:46:41      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:man   from   list   文件夹   home   project   mod   use   mapr   

 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"});
            });
}
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(一)

标签:man   from   list   文件夹   home   project   mod   use   mapr   

原文地址:http://www.cnblogs.com/Sam--Wang/p/6284181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!