Dear all i have the following controller,
[Route("[action]/{phone}/{password}", Name="PhoneLogin")]
[HttpGet]
public async Task<ActionResult<User>> PhoneLogin(string phone, string password)
{
var response = await _repository.PhoneLogin(phone, password);
if (response == null) { return NotFound(); }
return Ok(_mapper.Map<UserReadDto>(response));
}
public async Task<User> PhoneLogin(string phone, string pass)
{
StringCipher s = new StringCipher();
using (SqlConnection sql = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand("spPhoneLogin", sql))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@phone", phone));
cmd.Parameters.Add(new SqlParameter("@password", s.EncryptString(pass)));
User response = null;
await sql.OpenAsync();
using (var reader = await cmd.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
response = MapToValue(reader);
}
}
return response;
}
}
}
i’m new to API’s. i’m trying to send two parameters with the request.
and how is the URI constructed in that case.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Based on your routing attribute [Route("[action]/{phone}/{password}", Name="PhoneLogin")], the method can be reached under /PhoneLogin/anyString/anyOtherString where anyString would be bound to phone and anyOtherString to password.
If you have an additional route attribute on the controller class, such as [Route("[controller]")], the name of your controller also needs to be added which results in /MyControllerName/PhoneLogin/anyString/anyOtherString.
Please take a closer look at the documentation on model binding and routing fundamentals. The default model binding follows a predefined order, which (based on the documentation) is
- Form fields
- The request body (For controllers that have the [ApiController] attribute.)
- Route data
- Query string parameters
- Uploaded files
So since no form fields or request body is provided (which is the most common case for a get-request), the route data is used to bind the two parameters.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0