Exemplo n.º 1
0
        public async Task <IActionResult> Create(CreateVmViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }
            var cookieContainer = new CookieContainer();

            using (var handler = new HttpClientHandler {
                CookieContainer = cookieContainer
            })
                using (var client = new HttpClient(handler)
                {
                    BaseAddress = _baseAddress
                })
                {
                    var content = new FormUrlEncodedContent(new []
                    {
                        new KeyValuePair <string, string>("name", viewModel.Name),
                        new KeyValuePair <string, string>("type", viewModel.Type),
                        new KeyValuePair <string, string>("comment", viewModel.Comment)
                    });
                    var result = await client.PostAsync("/api/vm", content);

                    try
                    {
                        result.EnsureSuccessStatusCode();
                        return(RedirectToAction("All", "Vm"));
                    }
                    catch (HttpRequestException)
                    {
                        ModelState.AddModelError("", "Cannot create vm");
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError("", e.Message);
                    }
                    return(View(viewModel));
                }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Post(CreateVmViewModel viewModel)
        {
            try
            {
                Vm vm;
                if (viewModel.Type.Equals("ExtendedVm"))
                {
                    vm = await _vmManager.CreateExtendedVmAsync(viewModel.Name, viewModel.Comment);
                }
                else
                {
                    vm = await _vmManager.CreateVmAsync(viewModel.Name);
                }

                return(CreatedAtAction("Get", new { id = vm.Id }, vm));
            }
            catch (ArgumentException e)
            {
                ModelState.AddModelError("error", e.Message);
            }

            return(BadRequest(ModelState));
        }