Я пытаюсь передать несколько данных, используя метод http postasync client. В настоящее время я использую postasync на стороне клиента, а на стороне сервера у меня есть метод post, который принимает два параметра. Мой вопрос заключается в том, как мне успешно получить эту информацию на стороне сервера с помощью C #. Я хочу иметь возможность получить информацию для входа в систему, а также логическое значение islogin. Как мне получить его на стороне сервера.
Вот код сервера:
// POST: api/Register
[ResponseType(typeof(Login))]
public IHttpActionResult PostLogin(Login login, bool isLogin) {
if (!ModelState.IsValid) {
return BadRequest(ModelState);
}
using (SqlConnection conn = new SqlConnection(_dbConn))
{
conn.Open();
SqlCommand cmd = new SqlCommand("GetLogin", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter param = new SqlParameter();
cmd.Parameters.AddWithValue("@email", SqlDbType.VarChar).Value = login.Email;
cmd.Parameters.AddWithValue("@pwd", SqlDbType.VarChar).Value = login.Password;
SqlParameter returnValue = new SqlParameter("returnVal", SqlDbType.Int);
returnValue.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(returnValue);
cmd.ExecuteNonQuery();
var result = returnValue.Value;
if (isLogin == true)
{
//int result = db.GetLogin(login.Email, login.Password);
switch (result)
{
case (int)DBResponses.Success:
//return CreatedAtRoute("DefaultApi", new { id = 0 }, (int)DBResponses.Success);
return Ok((int)DBResponses.Success);
case (int)DBResponses.BadEmail:
//return CreatedAtRoute("DefaultApi", new { id = 2 }, (int)DBResponses.BadEmail);
return Ok((int)DBResponses.BadEmail);
case (int)DBResponses.BadPassword:
//return CreatedAtRoute("DefaultApi", new { id = 3 }, (int)DBResponses.BadPassword);
return Ok((int)DBResponses.BadPassword);
case (int)DBResponses.EmailNotVerified:
//return CreatedAtRoute("DefaultApi", new { id = 4 }, (int)DBResponses.EmailNotVerified);
return Ok((int)DBResponses.EmailNotVerified);
case (int)DBResponses.EmailNotFound:
//return CreatedAtRoute("DefaultApi", new { id = 5 }, (int)DBResponses.EmailNotFound);
return Ok((int)DBResponses.EmailNotFound);
default:
//NOTE: shouldn't reach this point
return Ok((int)DBResponses.GeneralFailure);
//return CreatedAtRoute("DefaultApi", new { id = 6 }, (int)DBResponses.GeneralFailure);
}
}
else
{
switch (result)
{
case (int)DBResponses.LoginEntryCreated:
//return CreatedAtRoute("DefaultApi", new { id = 1 }, (int)DBResponses.LoginEntryCreated);
return Ok((int)DBResponses.LoginEntryCreated);
default:
return Ok((int)DBResponses.GeneralFailure);
}
}
}
Вот код для клиентской стороны:
internal async Task<string> GetLoginAsync(Login login,bool isLogin) {
string json = JsonConvert.SerializeObject(login);
string json1 = JsonConvert.SerializeObject(isLogin);
HttpContent content = new StringContent(json);
content.Headers.Add("isLogin", isLogin.ToString());
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage result = await c.PostAsync(development, content);
string responseMessage = await result.Content.ReadAsStringAsync();
if (responseMessage == "0")
{
return "Success";
}
else if (responseMessage == "2")
{
return "BadEmail";
}
else if (responseMessage == "3")
{
return "BadPassword";
}
else if (responseMessage == "4")
{
return "EmailNotVerified";
}
else if(responseMessage =="5")
{
return "EmailNotFound";
}
//Should never reach this point
else {
return "GeneralFailure";
}
}