.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

.net core 入门一_第1张图片

 

二 连接mysql(使用EF)

1.nuget:MySql.Data.EntityFrameworkCore        Microsoft.EntityFrameworkCore

.net core 入门一_第2张图片.net core 入门一_第3张图片

 

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 options) : base(options) { }
        public DbSet 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(options => options.UseMySQL(conn));

            services.AddDbContext(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

.net core 入门一_第4张图片

 

三 部署到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

.net core 入门一_第5张图片

 

3.新建IIS网站

.net core 入门一_第6张图片

.net core 入门一_第7张图片

.net core 入门一_第8张图片

 

你可能感兴趣的:(.net core 入门一)