1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【保険商品管理システムの開発】MVCコントローラーとWeb API コントローラの違い

Posted at

間違っていた認識

Web API コントローラにPOSTMANでjsonデータを投入すると、MVCコントローラーを通してURLで表示されると勘違いしていました。

MVC コントローラと Web API コントローラの違い

LifeInsuranceMvcController は Controller を継承 → 返り値は View() が中心

• 例: return View(customers); → HTML にレンダリングされる。
• ブラウザで直接アクセスして表示するための画面向け。
• JSON を返すなら return Json(obj) や Web API のように ControllerBase を使う必要あり。

LifeInsuranceController(API)は ControllerBase + [ApiController]

• return Ok(obj) → JSON を返す。
• Postman や fetch などで利用する API 向け。
• 画面に依存しない処理の場合に使用する。

URLの違い

Web API コントローラ

https://localhost:7252/api/LifeInsuranceMvc/AddCustomer

「api」が付きます。

MVCコントローラ

https://localhost:7252/LifeInsuranceMvc/AddCustomer

「api」が付きません。

結論

今回は業務レベルの保険処理システムを作成することがテーマなので、データ投入画面から作成してMVCコントローラを作成する必要があります。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?