C# ASP.NET Core開発001.001

3416 ワード

Visual studio Code
  • は公式サイトにインストールします.NET Core SDK
  • cmd
  • dotnet new console --name HelloSSharp
    
  • download Visual studio code
  • プロジェクトフォルダを選択右クリックopen with code
  • using System;
    using System.IO;
    
    namespace HelloSSharp
    {
        class Program
        {
            static void Main(string[] args)
            {
                var stream = File.Create(@"E:\CSharbProject\HelloSSharp\test.txt");
                var writer = new StreamWriter(stream);
    
                Console.WriteLine("Hello World!");
                writer.WriteLine("1231");
                writer.Flush();
                writer.Close();
            }
        }
    }
    
    

    Visual studio 2019
  • Webアプリケーションモデルview controller
  • を作成
  • add model
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace WebApplication1.Models
    {
        public class Student
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }
    }
    
    
  • add controller
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using WebApplication1.Models;
    
    // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
    
    namespace WebApplication1.Controllers
    {
        public class StudentController : Controller
        {
            // GET: //
            public IActionResult Index()
            {
    
                
                var Students = new List
                {
                    new Student { ID = 1, Name = "adfa" },
                    
                    new Student { ID = 2, Name = "adfa" },
        
                    new Student { ID = 3, Name = "adfa" },
    
                    new Student { ID = 4, Name = "adfa" },
        
                    new Student { ID = 5, Name = "adfa" }
                };
                return View(Students);
            }
        }
    }
    
    
  • 右クリックadd view Template-modelフレームワーク自動生成フロントエンドコード
  • @model IEnumerable
    
    @{
        Layout = null;
    }
    
    
    
    
    
        
        Index
    
    
    

    Create New

    @foreach (var item in Model) { }
    @Html.DisplayNameFor(model => model.ID) @Html.DisplayNameFor(model => model.Name)
    @Html.DisplayFor(modelItem => item.ID) @Html.DisplayFor(modelItem => item.Name) @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
  • 運転
  • http://localhost:53819/student