在上一篇介绍中我们得知
.net core mvc 跟.net core web api 在专案C#语法跟预设架构差异
得知.net core web api 其实只着重於资料的请求回传不涉及到View检视跟View Model相关范畴
因此预设不会帮我们先准备Views跟Models的目录
基本上於.net core mvc透过GET跟POST就能满足一般很大宗的CRUD需求
但在web api这边则是分得更细
今天要介绍的部分是有关於HTTP资源操作共分为如下几种模式
HttpGet:负责获得资源
HttpPost:负责资料添加
HttpPut:负责更新资料(约定俗成但也可藉由POST),常用在更新整个资源(比方:整个特定model entity)
HttpDelete:负责删除资料
HttpHead:获取HTTP head部分的资讯(通常用void,不需要回传型态)
HttpOptions:用於获得自URI的资源在Request/Response过程可使用的功能选项。(可对ajax跨域请求做一些提前检查),跟HTTP Head有点类似但差异在於会回传Body内文。
HttpPatch:负责更新资料,常用在更新部分资源(比方:只更新特定model enity中的某些property),使用上ContentType发出请求必须用application/json-patch+json方式而非用form-data(後端会收不到)。
我们上一篇是用范例来测试跑api
这里我自己额外重新建立一个新的api controller
对Controllers 目录右键新增API控制器
点选加入
命名StudentController.cs
新增一个function
若没特别加特定属性标签修饰预设采用HttpGet
在此还是将其标注起来
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyApiTest1.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
[HttpGet]
public string Show()
{
return "Hi 你好";
}
}
}
在呼叫时直接网址输入 http://localhost:1914/api/Student
竟然可直接呼叫的到Show方法
原因在於目前api 控制器只存在唯一一个get请求的function
所以会有这种效果
那如果说要指定其方法名来访问有几种方式
法1.从Route调整
[Route("api/[controller]/[action]")]
此时呢就无法直接只输入控制器名来存取的到show方法了
就必须指定show
法2.从[HttpGet("[action]")] 来调整
法3.从[HttpGet("路由任意名称")] 来设置路由任意名称(不一定要跟action方法名一致)
比方我这里设置[HttpGet("Display")]
大小写没差
如果说我们想要设计一个.net core web api get请求可以
把一个DTO物件(model class)回传一json格式
再action method回传型态我们可以透过指定ActionResult<物件型别> 来达成
.net core webapi会自动对於Http Get操作将ActionResult来进行回传
且会自动把物件序列化为JSON格式的文本
这里新建一Models目录并新增如下Person class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyApiTest1.Models
{
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
}
在StudentController.cs中新增一个action method 如下
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyApiTest1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyApiTest1.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
[HttpGet("Display")]
public string Show()
{
return "Hi 你好";
}
[HttpGet("GetPerson")]
public ActionResult<Person> GetPerson()
{
var p = new Person
{
Id = 1,
Name = "Jason",
Age = 27,
Sex = "man"
};
return p;
}
}
}
呼叫观看结果就会看到被自动序列化为 json格式
而这是单一个物件回传
当我们想回传不只一个物件
可能物件List的时候
Action Method设计可以类似如下
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyApiTest1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyApiTest1.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
[HttpGet("Display")]
public string Show()
{
return "Hi 你好";
}
[HttpGet("GetPerson")]
public ActionResult<Person> GetPerson()
{
var p = new Person
{
Id = 1,
Name = "Jason",
Age = 27,
Sex = "man"
};
return p;
}
[HttpGet("GetPersons")]
public ActionResult<List<Person>> GetPersons()
{
List<Person> lsPersons = new List<Person>()
{
new Person(){Id=1,Name="Amy",Age=22,Sex="woman"},
new Person(){Id=2,Name="Jessica",Age=25,Sex="woman"},
new Person(){Id=3,Name="Banson",Age=24,Sex="man"}
};
return lsPersons;
}
}
}
呼叫观看结果就会看到被自动序列化为 json格式而且是中括号包起来的阵列型态
以上是针对HttpGet较常见的处里也简单带一些路由模板概念
本文同步更新至个人部落格
https://coolmandiary.blogspot.com/2021/09/net-core-web-api02.html
总结: 一切都是文件 在Linux里所有元素的操作(包括硬件)都是以文件表示. 统一的操作介面让程序...
如果你有约七个後端与资料库工程师,四个前端工程师,年资0到8年不等,大家合作从零开始打造这个产品,开...
DAY 5提到暂存器如何查找,还有开启时钟才能对GPIO口操作,我补充一下昨天没贴到的暂存器地图,在...
1. 如何确认资源是否载入 (e.g. css, js, API, ...) NetWork ex:...
重构也是需要时间的不是吗? 的确,重构也是需要额外时间的,但这应该是我们专业的一部份。一个好的重构时...