Blazor專案中建立WebApi
- Program.cs中新增程式碼:
//第一步 builder.Services.AddControllers(); ..... //第二步 app.MapControllers();
- 建立介面檔案
using Microsoft.AspNetCore.Mvc; namespace Niunan.Net.Charge.BlazorWeb.JieKou { [Route("/api/[controller]/[action]")] public class CommonController : Controller { //第三步 [HttpGet] public IActionResult GetCategory() { return Json(new { code = 0, msg = "success", data = "hello 牛腩" }); } } }
- 執行即可看到效果,http://localhost:5056/api/common/getcategory