public ActionResult CreateExpense()
 {
     ViewBag.Title = "Create new expense";
     Expense expense = new Expense();
     string name = System.Web.HttpContext.Current.User.Identity.Name;
     if (!string.IsNullOrEmpty(name))
     {
         var task = Task.Factory.StartNew<User>(() =>
         {
             Common.Models.User usr = _userRepository.Find(x => string.Compare(x.Name, name) == 0);
             return usr;
         });
         task.Wait();
         Session["UserIdValue"] = task.Result.Id;
     }
     return View(expense);
 }
        public ActionResult CreateExpense(Expense model)
        {
            if (ModelState.IsValid && model != null)
            {
                using (HttpClient client = new HttpClient())
                {
                    Uri uri = HttpContext.Request.Url;
                    string host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port + "/";

                    MvcApplication.Logger.InfoFormat("CreateExpense host address is: {0}", host);

                    if (!string.IsNullOrEmpty(host))
                    {
                        if (!host.EndsWith("/"))
                            host = string.Format("{0}/", host);
                        client.BaseAddress = new Uri(host);
                    }

                    client.DefaultRequestHeaders.Add("UserId", string.Format("{0}", Session["UserIdValue"]));
                    client.DefaultRequestHeaders.Add("authenticationToken", "1111");

                    var formatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();

                    using (HttpResponseMessage response = client.PostAsync<Expense>("api/Expense/Complex/", model, formatter).Result)
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            ViewBag.Message = "Data has been successfully submitted";

                            var resp1 = response.Content.ReadAsStringAsync().Result;
                            if (!string.IsNullOrEmpty(resp1))
                                ViewBag.Response = resp1;
                        }
                    }
                }

                return RedirectToAction("CreateExpense");
            }

            ViewBag.Message = "Fail to submit data";
            return RedirectToAction("CreateExpense");
        }
 private HttpResponseMessage ResponseFromPost(Expense expense)
 {
     var resp = new HttpResponseMessage(HttpStatusCode.Created);
     resp.Content = new StringContent(expense.UserId.ToString());
     resp.Headers.Location = new Uri(Url.Link("DefaultApi", new { action = "get", id = expense.Id }));
     return resp;
 }
        public HttpResponseMessage Put(int id, Expense expense)
        {
            if (expense == null)
                return new HttpResponseMessage(HttpStatusCode.BadRequest);

            InitializeUserId();
            try
            {
                var repo = _unity.GetRepository<Expense>();
                var ent = repo.FindSingle(x => x.Id == id && x.UserId == _userId);
                if (ent != null)
                {
                    repo.Update(ent);
                    _unity.Commit();
                    return new HttpResponseMessage(HttpStatusCode.OK);
                }
            }
            catch (Exception ex)
            {
                MvcApplication.Logger.Error("Error occurred while put Expense", ex);
                return new HttpResponseMessage(HttpStatusCode.ExpectationFailed);
            }

            return new HttpResponseMessage(HttpStatusCode.Created);
        }