.Net Core Web Api_筆記11_組合路由
在一個Action中倘若想實現多種結果返回或者多種輸入參數
就勢必要能夠支援多種路由模板
可能搭配不同權限身份會有不同路由網址
一個action配置多組路由模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace NetCoreApiTest1.Controllers { [Route("api/[controller]")] [ApiController] public class AccountController : ControllerBase { [Route("")] //api/account [Route("/")] //api/account/ [Route("Users")]//api/account/users public string Users() { return "users data..."; } } } |
不透過Controller層級的路由模板一律下放給各組action設置路由
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace NetCoreApiTest1.Controllers { //[Route("api/[controller]")] [ApiController] public class AccountController : ControllerBase { [Route("Account")] //api/account [Route("Account/")] //api/account/ [Route("Account/Users")]//api/account/users public string Users() { return "users data..."; } [Route("Users/{id}")] [Route("Users/Get/{id}")] public string GetUserById(string id) { return $"User Id: {id}"; } } } |
Controller層級跟Action層級都有組合路由情況
已下面情況來講就會共有六種路由模板的組合可以訪問
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace NetCoreApiTest1.Controllers { [Route("Account")] [Route("api/[controller]")] [ApiController] public class AccountController : ControllerBase { [Route("")] [Route("UserList")] [Route("GetUsers")] public string Users() { return "users data..."; } } } |
留言
張貼留言