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

來源:互聯網
上載者:User

標籤: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(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.