ASP.NET WebAPI Project Notes


  1. 說明
    1. WebAPI Pattern
    2. Restful API
  2. Identity
  3. 參考資料

跨入 ASP.NET WebAPI 專案開發的筆記。

logo

說明

WebApiConfig.cs

namespace WebAPIRepo
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API 設定和服務

            // Web API 路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

ValuesController.cs

public class ValuesController : ApiController
{
    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5
    public string Get(int id)
    {
        return "value";
    }

    // POST api/values
    public void Post([FromBody] string value)
    {
    }

    // PUT api/values/5
    public void Put(int id, [FromBody] string value)
    {
    }

    // DELETE api/values/5
    public void Delete(int id)
    {
    }
}

WebAPI Pattern

Pattern Info
REST
RPC
GraphQL
WebHook
WebSockets
HTTP Streaming

Restful API

Action Meaning
Get 取得資源
Post 新增資源
Put 替換與取代資源
Patch 資源的部分更新
Delete 移除資源

Identity

Bearer Token
JWT
Cookies
Digest Authentication
OAuth

參考資料

Web API 開發心得 (1) - WebForm 搭配 Web API

從無到有,打造一個漂亮乾淨俐落的 RESTful API

認識 OAuth 2.0:一次了解各角色、各類型流程的差異

RESTful web API design | learn.microsoft

Web API implementation | learn.microsoft