Web api Action从服务器上的android / iOS app调用两次

时间:2017-03-28 07:17:03

标签: c# android asp.net-mvc-4 asp.net-web-api asp.net-4.5

我面临一个棘手的问题。我有一个适用于Android和Android的Web API项目。 IOS。 每当我在服务器上部署我的代码时,一个特定的Action被调用两次&实际上从Android / iO应用程序执行两次后,它只被调用一次。

此行为向我显示数据已存在的错误。这是因为第一次执行已经存储了系统再次检查第二次呼叫的数据。执行。我们检查申请方。电话只进行一次。

我正在使用Visual Studio 2012,.net framework 4.5。

    [HttpPost]
    [ActionName("ActionName")]
    public HttpResponseMessage ActionName(object obj) 
    { 
       {




        if (obj.Key_Parameter != "" && obj.Key_Parameter != null)
        {
            App_Code.CheckApiKey check = new App_Code.CheckApiKey();
            bool result = check.CheckApi("Leave", "ApplyLeave", obj.Key_Parameter);
            if (result == false)
            {

                type = "Fail"; status = false; message = "Invalid Api Key,Please check!";
                HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotFound, new { type, status, message });
                return Invalid;
            }
            else
            {
                obj.DateFrom = obj.DateFrom.Replace('-', '/');
                obj.DateTo = obj.DateTo.Replace('-', '/');
                DateTime DateFrom = DateTime.ParseExact(obj.DateFrom, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                DateTime DateTo = DateTime.ParseExact(obj.DateTo, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                DateTime chkDate = DateTime.Today;
                if (obj.LeaveType == "PL" || obj.LeaveType == "CL" || obj.LeaveType == "SL")
                {
                    if (!Confirmed(Convert.ToInt64(obj.UserCode)))
                    {
                        type = "Fail"; status = false; message = "Can't Apply not confirmed";
                        HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotAcceptable, new { type, status, message });
                        return Invalid;
                    }
                }
                if (obj.LeaveType == "ML")
                {
                    if (obj.Gender.Trim() == "")
                    {
                        type = "Fail"; status = false; message = "Can't apply for ML !!!";
                        HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotAcceptable, new { type, status, message });
                        return Invalid;
                    }
                    if (obj.Gender.Trim() == "MALE")
                    {
                        type = "Fail"; status = false; message = "Only Females can apply for ML";
                        HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotAcceptable, new { type, status, message });
                        return Invalid;
                    }
                    if (GetDaysAfterConfirmation(obj) < 161)
                    {
                        type = "Fail"; status = false; message = "Can not apply for ML now. Please read the Leave Policy for Maternity leave for futher details";
                        HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotAcceptable, new { type, status, message });
                        return Invalid;
                    }
                }
                if (obj.LeaveType == "PL")
                {
                    if (GetDaysAfterConfirmation(obj) < 365)
                    {
                        type = "Fail"; status = false; message = "You can apply PL only after completion of 1 year";
                        HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotAcceptable, new { type, status, message });
                        return Invalid;
                    }
                }
                if (obj.Allow.Trim() == "N")
                {
                    int nDays = (chkDate - DateFrom).Days;

                                 }
                System.Threading.Thread.Sleep(5000);
                if (dataadded == false)
                {


                    if (CheckDate(DateFrom, DateTo, obj.UserCode) == false)
                    {

                        string tempdate1 = obj.DateFrom;
                        string tempdate2 = obj.DateTo;
                        temp123 = 4;
                                         type = "Fail"; status = false; message = "Period specified is not correct or overlapping.";
                        flag = false;
                        HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotAcceptable, new { type, status, message });

                        return Invalid;
                    }
                }
                if (obj.LeaveType.Trim() != "COMP" && obj.LeaveType.Trim() != "LWP" && obj.LeaveType.Trim() != "ML")
                {
                    if (HasBalance(obj) < Convert.ToDecimal(obj.Days))
                    {
                        type = "Fail"; status = false; message = "Not enough " + obj.LeaveType + " Balance ";
                        HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotAcceptable, new { type, status, message });
                        return Invalid;
                    }
                }
                if (obj.Days > 0 && obj.Days < 1)//Wrong Condition
                {
                    if (DateFrom != DateTo)
                    {
                        type = "Fail"; status = false; message = "Invalid Period Specified";
                        HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotAcceptable, new { type, status, message });
                        return Invalid;
                    }
                }

                if (Convert.ToDecimal(obj.Days) <= 0)
                {
                    type = "Fail"; status = false; message = " Number of days can't be zero ";
                    HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotAcceptable, new { type, status, message });
                    return Invalid;
                }
                if (obj.LeaveType == "PL")//Wrong Condition
                {
                    if (obj.Days < 3)
                    {
                        type = "Fail"; status = false; message = "Minimum days for PL should be equal to or more than 3 days";
                        HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotAcceptable, new { type, status, message });
                        return Invalid;
                    }
                }
                if (obj.LeaveType == "CL")
                {
                    if (obj.Days > 2)
                    {
                        type = "Fail"; status = false; message = "Casual leave can not be more than 2 days";
                        HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotAcceptable, new { type, status, message });
                        return Invalid;
                    }
                }
                if (obj.LeaveType == "ML")
                {
                    if (obj.Days > 84)
                    {
                        type = "Fail"; status = false; message = "ML can be taken for 12 weeks only ";
                        HttpResponseMessage Invalid = Request.CreateResponse(HttpStatusCode.NotAcceptable, new { type, status, message });
                        return Invalid;
                    }
                }

                if (temp123 != 1 && temp123 != 4 && flag)
                {
                    SqlConnection conz = new SqlConnection(conString);
                    try
                    {

                        string cmduz = "insert into z(datefrom,dateto,empcode,lremark,lyear,ldays,adddate,leavecode,status) values (@datefrom,@dateto,@empcode,@lremark,@lyear,@ldays,@adddate,@leavecode,@status) ";
                        SqlCommand cmda = new SqlCommand(cmduz, conz);
                        cmda.Parameters.Add("@datefrom", SqlDbType.SmallDateTime).Value = DateFrom;
                        cmda.Parameters.Add("@dateto", SqlDbType.SmallDateTime).Value = DateTo;//DateTo.SelectedDate;
                        cmda.Parameters.Add("EMPCODE", SqlDbType.Int).Value = obj.UserCode;
                        cmda.Parameters.Add("@LEAVECODE", SqlDbType.VarChar).Value = obj.LeaveType; //cmbLeaveType.SelectedValue;
                        cmda.Parameters.Add("@lremark", SqlDbType.VarChar).Value = obj.Remarks;//txtRemarks.Text;
                        cmda.Parameters.Add("@LYEAR", SqlDbType.Int).Value = DateFrom.Year;
                        cmda.Parameters.Add("@adddate", SqlDbType.DateTime).Value = System.DateTime.Now;
                        cmda.Parameters.Add("@ldays", SqlDbType.Decimal).Value = obj.Days;//txtDays.Value;
                        cmda.Parameters.Add("@status", SqlDbType.VarChar).Value = "Unapproved";
                        cmda.CommandType = CommandType.Text;
                        obj.ActFlag = "";

                        conz.Open();
                        cmda.CommandText = cmduz;

                        cmda.ExecuteNonQuery();

                        dataadded = true;
                        cmduz = "";

                        type = "Success"; status = true; message = "Leave Added Successfully" + dataadded + ".";
                        HttpResponseMessage ok = Request.CreateResponse(HttpStatusCode.OK, new { type, status, message });
                        //return ok;


                    }
                    catch (Exception ex)
                    {
                        type = "Exception";
                        status = false;
                        message = "Leave Added Successfully2";
                        HttpResponseMessage Excep = Request.CreateResponse(HttpStatusCode.ExpectationFailed, new { type, status, message });
                        return Excep;
                    }

                    try
                    {

                        App_Code.FcmPushNotification notification = new App_Code.FcmPushNotification();
                        string Message = "Leave Request From " + obj.Name;
                        notification.SendPushNotification(obj.PushUserCode, Message, "Applied");

                    }
                    catch
                    {

                    }
                    try
                    {
                        if (obj.Allow == "Y")
                        {

                            UpdateEmployeeMaster(Convert.ToInt64(obj.UserCode));
                            if (File.Exists(fileLoc))
                            {
                                string LogMsg = "Update Emplotee Master Function Executed.";
                                log(LogMsg);
                            }
                        }
                        string vmessage = " I would like to apply for " + obj.LeaveType + " From " + DateFrom.ToString("dd-MM-yyyy") + " to " + DateTo.ToString("dd-MM-yyyy") + " for " +/*txtDays.Text*/ obj.Days + " day(s). \n\n";

                        if (obj.Remarks.Trim() != "")
                        {
                            vmessage = vmessage + "\n\n Remarks: " + obj.Remarks;
                        }
                        vmessage = vmessage + "\n\n Kindly approve !!! at " + ApproveURL;

                        int vEmpCode = Convert.ToInt16(obj.Reporting);



                        string Mail_result = InformUser(vEmpCode, vmessage, "Leave Application Employee Code:" + obj.UserCode + " Name :" + obj.Name + " Date :" + System.DateTime.Now.ToString("dd-MM-yyyy", CultureInfo.InvariantCulture), obj);


                        if (Mail_result == "Success")
                        {
                            type = "Success"; status = true; message = "E-mail Sent Successfully";
                            HttpResponseMessage ok = Request.CreateResponse(HttpStatusCode.OK, new { type, status, message });

                            return ok;
                        }
                        else
                        {

                            type = "Success"; status = true; message = "E-mail could not be sent";
                            HttpResponseMessage Excep = Request.CreateResponse(HttpStatusCode.OK, new { type, status, message });
                            return Excep;
                        }

                        //  InformUser(vEmpCode, vmessage, "Leave Application Employee Code:" + obj.UserCode + " Name :" + obj.Name + " Date :" + System.DateTime.Now.ToString("dd-MM-yyyy"), obj);
                        // HttpResponseMessage ok = Request.CreateResponse(HttpStatusCode.OK, "Success !!");
                        // return ok;

                    }
                    catch (Exception ex)
                    {
                        type = "Success";
                        status = true;
                        message = "Email could not be sent";
                        HttpResponseMessage Excep = Request.CreateResponse(HttpStatusCode.ExpectationFailed, new { type, status, message });
                        return Excep;
                    }
                    finally
                    {

                        conz.Close();

                        conz.Dispose();


                    }
                }
            }
        }
        type = "Fail"; status = false; message = "fail";


        return Request.CreateResponse(HttpStatusCode.BadRequest, new { type, status, message });
    }

0 个答案:

没有答案