Web Api Routing in .NET Core
Full source code available here.
Routing in .NET Core Web Api (1.1 and 2) is a little different than in earlier versions.
I’ve written about this a few times, you can find those posts here.
In this post I’m going to show some examples of routing that might help you if you are having problems, especially if you want to have multiple GET methods. In a later article I will show how to use the action method selector to choose between actions methods with the same signature.
The code and comments are fairly self explanatory.
1[HttpGet("api/Person")]
2public class PersonController : Controller
3{
4 // GET: api/Person
5 [HttpGet]
6 public IEnumerable<string> Get()
7 {
8 return new string[] { "Dave Smith", "Edward Temple" };
9 }
10
11 // http://localhost:27624/api/person/1
12 [HttpGet("{id}")]
13 public string Get(int id)
14 {
15 return $"Get by id:{id} - Dave Smith";
16 }
17
18 // http://localhost:62689/api/person/ByNumber/5
19 [HttpGet("ByNumber/{number}")]
20 public string GetByNumber(int number)
21 {
22 return $"Get by number:{number} - Tom Davis";
23 }
24 // http://localhost:62689/api/person/ByFirstName/Steve
25 [HttpGet("ByFirstName")]
26 public string GetByFristName(string firstName)
27 {
28 return $"Get by first name - {firstName} Smith";
29 }
30
31 // http://localhost:62689/api/person/ByLastName?lastname=Smith
32 [HttpGet("ByLastName")]
33 public string GetByLastName(string lastName)
34 {
35 return $"Get by last name - Dave {lastName}";
36 }
37
38 // http://localhost:62689/api/person/ByFirstAndLastName?firstname=Steve&lastname=Smith
39 [HttpGet("ByFirstAndLastName")]
40 public string GetByFirstAndLastName(string firstName, string lastName)
41 {
42 return $"Get by first and last name - {firstName} {lastName}";
43 }
44}
Here are some more examples with attribute routing and a query model.
1[HttpGet("api/accounts/{AccountId}")]
2public class AccountsController : Controller
3{
4 // http://localhost:62689/api/accounts/11
5 public IActionResult Get(int accountId)
6 {
7 return Ok(accountId);
8 }
9
10 // http://localhost:62689/api/accounts/?accountId=11
11 // http://localhost:62689/api/accounts/?accountId=11&AccountName=dave
12 // http://localhost:62689/api/accounts/?accountId=11&AccountName=dave&managerid=15
13 [HttpGet("/api/accounts/")]
14 public IActionResult Get(QueryModel queryModel)
15 {
16 return Ok(queryModel);
17 }
18
19 // http://localhost:62689/api/accounts/11/manager/22
20 [HttpGet("Manager/{ManagerId}")]
21 public IActionResult Get(int accountId, int managerId)
22 {
23 return Ok($"accountId:{accountId}, managerId:{managerId}");
24 }
25
26 // http://localhost:62689/api/accounts/11/manager/22/role/33
27 [HttpGet("/api/accounts/{AccountId}/Manager/{ManagerId}/Role/{RoleId}")]
28 public IActionResult Get(int accountId, int managerId, int roleId)
29 {
30 return Ok($"accountId:{accountId}, managerId:{managerId}, roleId:{roleId}");
31 }
32}
33
34public class QueryModel
35{
36 public int AccountId { get; set; }
37 public string AccountName { get; set; }
38 public int ManagerId { get; set; }
39}
Full source code available here.