.Net 6 + WebApi Basics 10 - Routing and Passing Parameters
2023-01-30 23:29:01  .Net  >> .Net6  >> WebApi

Routing in .Net 6

Here is an example in the sample project.

1
2
3
4
5
6
7
8
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

[Route("[controller]")] is the route in the URL

controller is a placeholder for the function name, WeatherForecastController.

In Swagger page, this controller will get the URL as:
Alt text

The route, WeatherForecast, is the URL route path of controller’s name, WeatherForecastController, removes the placeholder, controller.

Route Calls Functions

Add the Get(), which returns the temperature information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[ApiController]
[Route("controller/[action]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

[ApiExplorerSettings(GroupName = "V1", IgnoreApi = false)]
[HttpPost]
public IEnumerable<WeatherForecast> Get(WeatherForecast weather)
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}

Add [action] into the Route attribute as [Route("controller/[action]")].

The route will be directed to https://localhost:7048/controller/Get

Alt text

Multiple Functions’ Call

Except for using [action], there are two other ways to call function through attributes.

  1. [HttpGet("xxx")]

    1
    2
    3
    4
    5
    6
    7
    //USE-->[HttpGet("xxx")]

    [HttpGet("xxx")]
    public string GetData()
    {
    return "1";
    }
  2. [Route("xxx")]

    1
    2
    3
    4
    5
    6
    7
    8
    //USE-->[Route("xxx")]

    [HttpGet]
    [Route("xxx")]
    public string GetData()
    {
    return "1";
    }

URL is https://localhost:7048/controller/xxx.

Alt text

Passing Parameters

There are six ways to transfer parameters through the functions.

1
2
3
4
5
6
FromBody  //application/json, generally pass a model class body
FromForm //incoming data from a submitted form sent by the content type
FromHeader //generally on model properties
FromQuery //generally on model itself.
FromRoute //From the route
FromServices //from services that is going to be injected.

Use following example to explain, GetList() will retrive all of the data stored in the database.
DBTestController.cs

1
2
3
4
5
6
7
8
9
/// <summary>
/// Get data from DB
/// </summary>
/// <returns></returns>
[HttpGet]
public List<UserClass> GetList()
{
return UserServices.GetUsers();
}

FromQuery

Get parameter from query by using FromQuery

1
2
3
4
5
6
7
8
9
10
/// <summary>
/// Get data from DB FromQuery
/// [FromQuery]
/// </summary>
/// <returns></returns>
[HttpGet]
public List<UserClass> GetList(string id)
{
return UserServices.GetUsers();
}

FromQuery is also the default way to get parameter from function.You can also specify the method by:

1
2
3
4
5
[HttpGet]
public List<UserClass> GetList([FromQuery]string id)
{
return UserServices.GetUsers();
}

Result:
截图

Assign the parameter id with the value of 123 will gain the request URL as:

https://localhost:7048/api/DbTest?id=123

The parameter is appended with a ?id=.

FromRoute

1
2
3
4
5
6
7
8
9
10
/// <summary>
/// Get data from DB FromRoute
/// [FromRoute ]
/// </summary>
/// <returns></returns>
[HttpGet("{id}")]
public List<UserClass> GetList([FromRoute] string id)
{
return UserServices.GetUsers();
}

Result:
截图

Compared with FromQuery, FromRoute uses the router information from the attribute,

[HttpGet("{id}")].

And the final URL is also different.
https://localhost:7048/api/DbTest/123

The parameter is appended with a /.


Next: .Net 6 + WebApi Basics 11

Last: .Net 6 + WebApi Basics 9


Good Day
😎