简体   繁体   English

使用 http get 请求 ASP.core API 传递多个参数

[英]pass multiple parameters with http get request ASP.core API

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.我是 API 的新手。 i'm trying to send two parameters with the request.我正在尝试随请求发送两个参数。 and how is the URI constructed in that case.以及在这种情况下如何构造 URI。

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 .根据您的路由属性[Route("[action]/{phone}/{password}", Name="PhoneLogin")] ,可以在/PhoneLogin/anyString/anyOtherString访问该方法,其中anyString将绑定到电话anyOtherStringpassword

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 .如果您在控制器类上有一个额外的路由属性,例如[Route("[controller]")] ,则还需要添加您的控制器的名称,这将导致/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默认模型绑定遵循预定义的顺序,(基于文档)是

  1. Form fields表单域
  2. The request body (For controllers that have the [ApiController] attribute.)请求正文(对于具有 [ApiController] 属性的控制器。)
  3. Route data路线数据
  4. Query string parameters查询字符串参数
  5. 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.因此,由于没有提供表单字段或请求正文(这是获取请求的最常见情况),因此使用路由数据来绑定两个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM