Return error message from Web API controller in ASP.Net

Last Reply 19 days ago By dharmendr

Posted 19 days ago

Return error message if condition not valid webapi

I want if my condition is not valid so user can see message "IP is allow to use this method"

        [Route("api/CustomerAPI/GetCustomers")]
        [HttpPost]
        public List<UserModel> GetCustomers()
        {
            List<UserModel> user = new List<UserModel>();
            string ip =ws.GetIpValue();

            if (ip == "Invalid")
            {
                error();
            }
            else
            {               
                string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constring))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT * FROM users", con))
                    {
                        cmd.CommandType = CommandType.Text;
                        con.Open();
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {
                            while (sdr.Read())
                            {
                                user.Add(new UserModel
                                {
                                    username = sdr["username"].ToString(),
                                    password = sdr["password"].ToString()
                                });
                            }
                        }
                        con.Close();                        
                    }
                }
                
            }
            return user;
        }

please advice

You are viewing reply posted by: dharmendr 19 days ago.
Posted 19 days ago

Hi nauna,

Since your api returning List<UserModel> you can validate after getting the response.

In code check if GetCustomers() count equals to zero then show the message IP is allow to use this method.

Or you can add a property to the that will be used to set the message and return from the api so that it can be checked while consuming.

Like below example.

[Route("api/CustomerAPI/GetCustomers")]
[HttpPost]
public List<UserModel> GetCustomers()
{
    List<UserModel> user = new List<UserModel>();
    string ip = ws.GetIpValue();

    if (ip == "Invalid")
    {
        user.Add(new UserModel {message= "IP is allow to use this method" });
    }
    else
    {
        string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM users", con))
            {

                cmd.CommandType = CommandType.Text;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        user.Add(new UserModel
                        {

                            username = sdr["username"].ToString(),
                            password = sdr["password"].ToString()
                        });
                    }
                }
                con.Close();

            }
        }

    }
    return user;
}

public class UserModel
{
    public string username { get; set; }
    public string password { get; set; }
    public string message { get; set; }
}

Then check in code like below.

if (GetCustomers()[0].message == "IP is allow to use this method")
{
    error();
}