Web API Post方法保持循环

时间:2015-11-04 11:57:25

标签: c# asp.net-web-api

我的控制器中有一个post方法,当我从客户端应用程序发送请求时,它正确地完成了工作。唯一的问题是,该方法不断循环,我不知道是什么原因。我尝试调试,如果我的foreach循环与它有关,但我相信情况并非如此。我对web api不是很熟悉,我需要你的帮助。

这是我的帖子方法:

[HttpPost]
        public HttpResponseMessage PostMain(IList<IMF_Main> mainFromConsolidator)
        {
            if (!ModelState.IsValid)
                return Request.CreateResponse(HttpStatusCode.BadRequest, 2);

            using (var anthill = new AnthillConsolidatorEntities())
            {
                if (anthill.IMF_Session.Where(w => w.TableinSession == "IMF_Main" && w.Session == 1).Count() > 0)
                    return Request.CreateResponse(HttpStatusCode.Conflict, 3);

                if (anthill.IMF_Session.Where(w => w.TableinSession == "IMF_Main").Count() == 0)
                {
                    var session = new IMF_Session();
                    session.TableinSession = "IMF_Main";
                    session.DateandTime = DateTime.Now;
                    session.Session = 1;
                    anthill.IMF_Session.Add(session);
                    anthill.SaveChanges();
                }

                var main = new IMF_Main();
                foreach (var item in mainFromConsolidator)
                {
                    if (anthill.IMF_Main.Where(w => w.ItemID == item.ItemID).Count() == 0)
                    {
                        main.BrandID = item.BrandID;
                        main.ItemID = item.ItemID;
                        main.CategoryID = item.CategoryID;
                        main.SubCategoryID = item.SubCategoryID;
                        main.ClassID = item.ClassID;
                        main.GenderID = item.GenderID;
                        main.CoaID = item.CoaID;
                        main.SubCoaID = item.SubCoaID;
                        main.First_SRP = item.First_SRP;
                        main.Current_SRP = item.Current_SRP;
                        main.Previous_SRP = item.Previous_SRP;
                        main.isSenior = item.isSenior;
                        main.isActive = item.isActive;
                        main.DateCreated = item.DateCreated;
                        anthill.IMF_Main.Add(main);
                        anthill.SaveChanges();
                    }
                }

                anthill.IMF_Session.Remove(anthill.IMF_Session.Where(w => w.TableinSession == "IMF_Main" && w.Session == 1).FirstOrDefault());
                anthill.SaveChanges();
            }
            return Request.CreateResponse(HttpStatusCode.OK, 1); 
        }

0 个答案:

没有答案