.net core 入门
Posted ligenyun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.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网站
以上是关于.net core 入门的主要内容,如果未能解决你的问题,请参考以下文章
第一个,net core项目,.net core入门介绍!!!
ASP.NET Core MVC 入门到精通 - 3. 使用MediatR
ASP.NET Core 入门教程 1使用ASP.NET Core 构建第一个Web应用