Visual studio Code
- 去官网安装.NET Core SDK
- cmd
dotnet new console --name HelloSSharp
- download Visual studio code
- 选中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
- 创建一个Web Application model 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: /<controller>/
public IActionResult Index()
{
var Students = new List<Student>
{
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<WebApplication1.Models.Student>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.ID)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</tbody>
</table>
</body>
</html>
- 运行
http://localhost:53819/student