1. .Net Core Fundamentals (.net core v1.1.2)

1.1. Get Started

Follow Official website: https://docs.microsoft.com/en-us/aspnet/core/getting-started

1.2. Adding a Configuration Source

Asp.net core no longer uses web.config as configuration file. Instead, appsettings.json is used for this purpose.

1.3. Features of Asp.net core

  1. Modular version of .NET Framework
  2. supports different environments
  3. cross-platform
  4. better performance
  5. Dependency Injection (Loose coupling, less code changes, better testability)

1.4. Startup.cs -- Entry point

  1. ConfigureServices is used to add services to the container, and to configure those services, Dependency Injection!

    • AddTransient: Every request creates an instance. 每次请求都创建一个新的实例. always different; a new instance is provided to every controller and every service.
    • AddScoped: the same within a request, but different across different requests. 相同请求得到同一个实例,常用注册 Repository。不同请求实例不同
    • AddSingleton: the same for every object and every request
  2. Configure is used to specify how an ASP.NET application will respond to individual HTTP requests. Use middleware to configure the HTTP request pipeline

1.5. Request Pipeline & Middleware

Request --> Middleware --> Middleware --> ... --> Response

Application Startup

1.6. ViewComponents VS PartialView

ViewComponent is a upgrade of PartialView.

Partial View:

Usually contains only html code and the model which has to be the one passed in calling page. Render PartialView in Parent View: @Html.Partial("partialview", model)

ViewComponent:

Contains logic! It has a Invoke method which will be called and render its ViewComponent

using System.Collections.Generic;
using Aspnetcore.Pieshop.Webapp.Models;
using Aspnetcore.Pieshop.Webapp.ViewModels;
using Microsoft.AspNetCore.Mvc;

namespace Aspnetcore.Pieshop.Webapp.ViewComponents
{
    public class ShoppingCartSummaryViewComponent : ViewComponent
    {
        private readonly ShoppingCart _shoppingCart;

        public ShoppingCartSummaryViewComponent(ShoppingCart shoppingCart)
        {
            _shoppingCart = shoppingCart;
        }

        public IViewComponentResult Invoke()
        {
            var items = _shoppingCart.GetShoppingCartItems();
            _shoppingCart.ShoppingCartItems = items;

            var shoppingCartViewModel = new ShoppingCartViewModel
            {
                ShoppingCart = _shoppingCart,
                ShoppingCartTotal = _shoppingCart.GetShoppingCartTotal()
            };
            return View(shoppingCartViewModel);
        }
    }
}

Parent View which called the ViewComponent:

// Maybe in _Layout.cshtml
@await Component.InvokeAsync("ShoppingCartSummary")

1.7. MVC cons and pros

pros:

  1. Separation of Concerns
  2. testability
  3. reuse

cons:

View may change too fast and Model cannot keep pace with View. Usually have to create ViewModels for View. This is painful as application get larger and larger.

1.8. Web Api in .Net Core

Post:

Use POST for creating a resource

  • 201 Created
  • Header: content-type

Validation

  • Data annotations
  • ModelState

Put/Patch:

Use PUT for full updates, PATCH for partial updates

  • JsonPatch standard
  • 204 NoContent or 200 Ok

Delete:

DELETE is for deleting resources

  • 204 NoContent
Copyright © Guanghui Wang all right reserved,powered by GitbookFile Modified: 2019-08-25 13:56:34

results matching ""

    No results matching ""