示例#1
0
    void SendResultInfoAsJson(GetAllEmployeesResponse res)
    {
        string strJson = JsonConvert.SerializeObject(res);

        Response.ContentType = "application/json; charset=utf-8";
        Response.AppendHeader("Access-Control-Allow-Origin", "*");
        Response.Write(strJson);
        Response.End();
    }
        public void TestGet()
        {
            EmployeesController employeesController = CreateController();

            IActionResult actionResult = employeesController.Get();

            // Checks if the result is OK
            Assert.IsType <OkObjectResult>(actionResult);

            OkObjectResult result = actionResult as OkObjectResult;

            // Checks if the result value is the correct response type.
            Assert.IsType <GetAllEmployeesResponse>(result.Value);

            GetAllEmployeesResponse response = result.Value as GetAllEmployeesResponse;

            // Checks if the controller returned the correct employees.
            Assert.Equal(4, response.Employees.Count());
        }
示例#3
0
    protected void Page_Load(object sender, EventArgs e)
    {
        GetAllEmployeesRequest  req;
        GetAllEmployeesResponse res = new GetAllEmployeesResponse();

        res.error = String.Empty;

        // 1. Deserialize the incoming Json.
        try
        {
            req = GetRequestInfo();
        }
        catch (Exception ex)
        {
            res.error = ex.Message.ToString();

            // Return the results as Json.
            SendResultInfoAsJson(res);
            return;
        }

        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        try
        {
            connection.Open();

            string     getAllEmps    = @"SELECT FirstName, LastName, Phone, JobType FROM Employee WHERE StoreID = @StoreID AND Status = 0";
            SqlCommand getAllEmpsCmd = new SqlCommand(getAllEmps, connection);
            getAllEmpsCmd.Parameters.Add("@StoreID", SqlDbType.Int);
            getAllEmpsCmd.Parameters["@StoreID"].Value = req.StoreID;

            SqlDataReader reader = getAllEmpsCmd.ExecuteReader();
            res.employees = new List <Employee>();
            while (reader.Read())
            {
                Employee curE = new Employee();
                curE.FirstName = Convert.ToString(reader["FirstName"]);
                curE.LastName  = Convert.ToString(reader["LastName"]);
                curE.Phone     = Convert.ToString(reader["Phone"]);
                curE.JobType   = Convert.ToInt32(reader["JobType"]);

                res.employees.Add(curE);
            }

            reader.Close();
        }
        catch (Exception ex)
        {
            res.error = ex.Message.ToString();
        }
        finally
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
        }

        // Return the results as Json.
        SendResultInfoAsJson(res);
    }