1. 创建项目

  • VSCode 打开准备用来存放项目文件夹的文件夹;
  • VSCode 中打开终端,运行命令:
    dotnet new webapi -o HelloRestful

2. 添加自定义逻辑

  • Controllers 同级)新建一个 Models 文件夹;
  • 在 Models 目录下新建 EmployeeInfo.cs 类,代码如下:
    using System;
    
    namespace HelloRestful.Controllers
    {
        public class EmployeeInfo
        {
            public int EmployeeNo { get; set; }
            public string EmployeeName { get; set; }
            public int Age { get; set; }
            public int Sex { get; set; }
            public string Position { get; set; }
            public bool IsActive { get; set; } = false;
            public DateTime CreateDate { get; set; } = DateTime.Now;
        }
    }
    View Code
  • 当对话框询问是否要将所需资产添加到项目时,选择“是”。
  • 在 Controllers 目录下新建 EmployeeInfoController.cs 类,代码如下:
    using System.Collections.Generic;
    using Microsoft.AspNetCore.Mvc;
    
    namespace HelloRestful.Controllers
    {
        [Route("api/[controller]")]
        [ApiController]
        public class EmployeeInfoController
        {
            private List<EmployeeInfo> employeeInfos = new List<EmployeeInfo>
            {
                new EmployeeInfo{
                    EmployeeNo = 1,
                    EmployeeName = "Jack",
                    Age = 21,
                    Sex = 1,
                    Position = "Front-end Development",
                },
                new EmployeeInfo{
                    EmployeeNo = 2,
                    EmployeeName = "Tom",
                    Age = 23,
                    Sex = 0,
                    Position = "Front-end Development",
                },
                new EmployeeInfo{
                    EmployeeNo = 3,
                    EmployeeName = "Martin",
                    Age = 22,
                    Sex = 1,
                    Position = ".Net Development",
                },
            };
    
            [HttpGet]
            public IEnumerable<EmployeeInfo> Get()
            {
                return employeeInfos;
            }
        }
    }
    View Code

3. 配置 IP 地址和 端口号

  • 打开 Properties/launchSettings.json 文件;

添加允许跨域配置

  • Startup.cs 文件;
  • ):
    readonly string AllowRequestOrigins = "_allowRequestOrigins";
  • Startup 类的 ConfigureServices() 方法中添加允许访问的域,代码如下:
    services.AddCors(options =>
                {
                    options.AddPolicy(AllowRequestOrigins, builder =>
                    {
                        // 允许访问的域,可添加多个
                        builder.WithOrigins("http://localhost:9080").AllowAnyHeader().AllowAnyMethod();
                    });
                });
    View Code
  • app.UseRouting(); 和 app.UseEndpoints(); 之间添加代码如下:
    app.UseCors(AllowRequestOrigins);

5. 运行服务,并测试能否正确输出数据

  • VSCode 终端中运行命令:
    cd HelloRestful
    dotnet run

   回车,输出如下如所示:

  • 如下:

 

 

 

 

 

 

教程:使用 ASP.NET Core 创建 Web API

.Net Core5 WebAPI + Vue + Axios 搭建一个跨域访问的例子

无法在 macOS 上启动 ASP.NET Core gRPC 应用

 

 

 

 

相关文章:

  • 2021-08-16
  • 2021-10-30
  • 2022-12-23
  • 2021-12-23
  • 2022-12-23
  • 2021-11-02
猜你喜欢
  • 2021-09-19
  • 2022-12-23
  • 2021-09-30
  • 2022-12-23
  • 2018-11-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案