Mark asp.net web api的基本概念
-
Upload
learningtech -
Category
Technology
-
view
461 -
download
2
Transcript of Mark asp.net web api的基本概念
ASP.NET Web API 的基本概念Mark Chen
What is ASP.NET Web API ?ASP.NET Web API is a framework that makes it
easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices.
ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
ASP.NET Web API 使用的是 HTTP Application Protocol 。
ASP.NET Web API 就是簡單的 HTTP 服務。
04/12/2023
2
HTTP Method 在 RESTful Web Service 中的典型應用
Operation HTTP Method http://example.com/resources/ http://example.com/resources/
123
Create POST 新增 / 追加一組新的 Resources 新增 / 追加一個新的 Resource
Read GET取得 Resources 集合中的所有詳細內容 取得指定 Resource 的詳細內容
Update PUT 以新的 Resources 更新目前的Resources
更新指定的某個 Resource ,如果指定的Resource 不存在,就會新增一個
Delete DELETE 刪除整組 Resources 刪除指定的 Resource
04/12/2023
3
REST 是一種設計風格,不是一種標準或協定, REST 的核心概念就是「如何正確地使用網頁標準 (Web Standards) 」。
RESTful Web API 是一個使用 HTTP 協定並遵循 REST 原則的 Web Service 。
Controller→ 在 ASP.NET MVC 與在 ASP.NET Web API 之間的區別
Controller 所繼承的 Class 不同,那麼Controller 內的 Action 所處理的行為模式也不相同: MVC Controller 根據 Action 所定義的行為模式
回傳 View 與資料 Web API 的 ApiController ,它的 Action 不會回
傳 View ,而是回傳一組資料 (XML, JSON…)
提供 Web API Service 的 Controller 一定是繼承自 ApiController
04/12/2023
4
ASP.NET Web API 的兩大主軸Models
在 Repository Patten 中定義資料存取的運作基礎,把所有存取資料庫的 CRUD 方法都集中在 Repository Class 內
ApiControllers把如何處理 Model 的方法封裝成一個一個的 API 方法,只要透過 API
方法之間的約定去呼叫 API 方法,就能夠以相當便利的方式完成 CRUD的操作
04/12/2023
5
ASP.NET Web API 與 Route (1/2)• Web API 使用 HTTP Methods (GET, POST, PUT, DELETE…) 來選
擇 Action ,而不是使用 URI 路徑來選擇 Action 。• Web API 是使用 API Controller Class 來處理 HTTP Request ,當
Web API 接收到一個 HTTP Request 時,會將此 HTTP 請求 Route 到一個 API 方法。 Route 規則表會決定哪一個 API 會被使用。如果沒有符合的 Route ,那個在 Client 端會收到一個 HTTP 404 的錯誤狀態。
• ASP.NET Web API 是看 ApiController Name 來決定, ASP.NET MVC 則是看 Controller Name 與 Action Name 來決定
04/12/2023
6
Web API 與 Route (2/2)
04/12/2023
7
name: 是要對應的 Route 名稱routeTemplate: Route 的範本defaults: 包含預設 Route value 的 object
URI: api/products/123 api/products?id=123
URI: products/123符合 不符合
WebApiConfig
ProductsController
參考 網站:
Creating a Web API that Supports CRUD Operations http://
www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations
Routing and Action Selection http://
www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection
Routing in ASP.NET Web API http://
www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
書籍: ASP.NET MVC 4 網站開發美學
04/12/2023
8