Web API学习笔记(七)——RESTful URLs和Action method return types

1.RESTful URLs

RESTful URLs是使用Http动词和URL定义的。
例:
GET + http//example.com/Employees

1.1 RESTful Get URLs

GET + domain.com/employees
获取所有employees的清单

GET + domain.com/employees/{employeeId}
获取单个employee的详情

GET + domain.com/employees/{employeeId} /accounts
获取单个employee的账户详情

GET + domain.com/Employees/{employeeId} /accounts/{accountID}
获取单个employee的单个账户详情

1.2 RESTful Post URLs

Post + domain.com/employees
Body:Account object

Add a new account for the employee

Post + domain.com/employees/{employeeId}/accounts
Body:Account object

Add a new account for the employee

1.3 RESTful Put URLs

Put + domain.com/employees/{employeeId}
Body:Account object
更新 employee

Post + domain.com/employees/{employeeId}/accounts/{accountId}
Body:Account object
更新特定员工的特定账户

1.4 RESTful Delete URLs

Delete + domain.com/employees/{employeeId}
删除 employee

Post + domain.com/employees/{employeeId}/accounts/{accountId}
删除特定员工的特定账户

2.Action method return types

2.1 特定类型(Specific type)

也就是在事件方法中返回一个特定类型。

按如下层级创建一个Models文件夹,在创建一个EmployeeModel.cs脚本
在这里插入图片描述
EmployeeModel.cs脚本代码如下

using System;
namespace MyWebApi.Models
{
    public class EmployeeModel
    {
        public int id { get; set; }
        public string name { get; set; }
    }
}

2.1.1对象形式

创建一个Employee控制器,EmployeeController.cs脚本代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWebApi.Models;

namespace MyWebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        public EmployeeModel GetEmployees()
        {
            return new EmployeeModel() { id = 13,name = "Lice"};
        }
    }
}

运行结果:
在这里插入图片描述

2.1.2 列表形式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWebApi.Models;

namespace MyWebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        public List<EmployeeModel> GetEmployees()
        {
            return new List<EmployeeModel>()
            { new EmployeeModel() { id = 13, name = "Lice" },
              new EmployeeModel() { id = 18, name = "Mick" }};
        }
    }
}

运行结果:
在这里插入图片描述

2.1.3 迭代形式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWebApi.Models;

namespace MyWebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        public IEnumerable<EmployeeModel> GetEmployees()
        {
            return new List<EmployeeModel>()
            { new EmployeeModel() { id = 13, name = "Lice" },
              new EmployeeModel() { id = 17, name = "Mick" }};
        }
    }
}

运行结果:在这里插入图片描述

2.1.4 异步迭代器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWebApi.Models;

namespace MyWebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        public async IAsyncEnumerable<EmployeeModel> GetEmployees()
        {
            
             yield return new EmployeeModel() { id = 17, name = "Mick" };
              await Task.Delay(1);
        }c
    }
}

运行结果:在这里插入图片描述

2.2 IActionResult

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWebApi.Models;

namespace MyWebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        [Route("")]
        public async IAsyncEnumerable<EmployeeModel> GetEmployees()
        {
            
             yield return new EmployeeModel() { id = 17, name = "Mick" };
              await Task.Delay(1);
        }

        [Route("{id}")]
        public IActionResult GetEmployees(int id)
        {
            if(id == 0)
            {
                return NotFound();
            }
            return Ok(
                new List<EmployeeModel>()
                {
                    new EmployeeModel() { id = 1, name = "Lice" },
                    new EmployeeModel() { id = 2, name = "Mick" }
                });

        }
    }
}

运行结果:
id = 0
在这里插入图片描述
id = 1
在这里插入图片描述
可以看到id = 0 时到状态码为404,id = 1时到状态码为200

2.3 带类型ActionResult(ActionResult< T > )

        [Route("{id}/base")]
        public ActionResult<EmployeeModel> GetEmployeesBase(int id)
        {
            if (id == 0)
            {
                return NotFound();
            }

            return Ok(new EmployeeModel() { id = 1, name = "Lice" });
        }

运行结果:
在这里插入图片描述
如图所示请求成功状态码为200

下一节我门就来讨论一下状态码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值