• .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-publishpublish

    3.新建IIS网站

  • 相关阅读:
    select.poll,epoll的区别与应用
    hibernate生成查询语句但查不到数据
    优化exp/imp导入导出速度大全
    完美逆向百度手机助手5.0底部菜单栏
    C#序列化和反序列化
    Centos6 编译安装局域网NTP服务器
    linux查看服务器型号
    fopen/fclose
    C文件操作之写入字符串到指定文件并在屏幕显示
    Centos6.x X64 飞信安装
  • 原文地址:https://www.cnblogs.com/ligenyun/p/11180090.html
Copyright © 2020-2023  润新知