.net core 入门一
官网教程:https://docs.microsoft.com/zh-cn/aspnet/core/getting-started/?view=aspnetcore-3.0&tabs=windows
目录:
1.新建项目.NET CORE API
2.连接mysql(使用EF)
3.部署到windows-IIS
一 新建项目.NET CORE API
二 连接mysql(使用EF)
1.nuget:MySql.Data.EntityFrameworkCore Microsoft.EntityFrameworkCore
using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace LibraryModel { [Table("School")] public class SchoolModel { [Key] public int Id { get; set; } public string SchoolName { get; set; } } }
using LibraryModel; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace LibrarySystem.DB { public class LibraryContext : DbContext { public LibraryContext(DbContextOptions<LibraryContext> options) : base(options) { } public DbSet<SchoolModel> School { get; set; } } }
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Threading.Tasks; using LibrarySystem.DB; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace LibrarySystem { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var conn = Configuration.GetSection("ConnectionString:Default").Value; services.AddDbContext<LibraryContext>(options => options.UseMySQL(conn)); services.AddDbContext<TodoContext>(opt => opt.UseInMemoryDatabase("TodoList")); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseMvc(); } } }
{ "Logging": { "LogLevel": { "Default": "Warning" } }, "ConnectionString": { "Default": "server=127.0.0.1;userid=root;password=xxxx;port=3306;database=world;allowuservariables=True;" }, "AllowedHosts": "*" }
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using LibraryModel; using LibrarySystem.DB; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace LibrarySystem.Controllers { [Route("api/[controller]")] [ApiController] public class SchoolController : ControllerBase { LibraryContext context; public SchoolController(LibraryContext _context) { context = _context; } [HttpGet] [Route("Query")] public dynamic Query(int id) { var data = context.School.FirstOrDefault(); return data; } [HttpGet] [Route("dosome")] public dynamic Dosome() { return "1111111"; } [HttpGet] public dynamic GetList() { var data = context.School.ToList(); return data; } } }
2.浏览器访问:https://localhost:5001/api/school/query
三 部署到windows-IIS
官网教程:https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.0
1.安装 .NET Core 托管捆绑包 dotnet-hosting-2.2.6-win.exe ,地址:https://download.visualstudio.microsoft.com/download/pr/a9bb6d52-5f3f-4f95-90c2-084c499e4e33/eba3019b555bb9327079a0b1142cc5b2/dotnet-hosting-2.2.6-win.exe
2.项目发布。存在地址:D:\core-publish\publish
3.新建IIS网站
天生我材必有用,千金散尽还复来
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)