C# ASP.NET Core开发 001.001

Visual studio Code

  1. 去官网安装.NET Core SDK
  2. cmd
dotnet new console --name HelloSSharp
  1. download Visual studio code
  2. 选中project文件夹右击, 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

  1. 创建一个Web Application model view controller
  2. 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; }
    }
}

  1. 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);
        }
    }
}

  1. 右击 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 */ })
  1. 运行
http://localhost:53819/student

你可能感兴趣的:(c#)