예제 #1
0
        private data.AspNetUserRoles GetById(int?id)
        {
            data.AspNetUserRoles aux = new data.AspNetUserRoles();
            using (var cl = new HttpClient())
            {
                cl.BaseAddress = new Uri(baseurl);
                cl.DefaultRequestHeaders.Clear();
                cl.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage res = cl.GetAsync("api/AspNetUserRoles/" + id).Result;

                if (res.IsSuccessStatusCode)
                {
                    var auxres = res.Content.ReadAsStringAsync().Result;
                    aux = JsonConvert.DeserializeObject <data.AspNetUserRoles>(auxres);
                }
            }
            return(aux);
        }
예제 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,UserId,RoleId")] data.AspNetUserRoles aspNetUserRoles)
        {
            if (id != aspNetUserRoles.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    using (var cl = new HttpClient())
                    {
                        cl.BaseAddress = new Uri(baseurl);
                        var content     = JsonConvert.SerializeObject(aspNetUserRoles);
                        var buffer      = System.Text.Encoding.UTF8.GetBytes(content);
                        var byteContent = new ByteArrayContent(buffer);
                        byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                        var postTask = cl.PutAsync("api/AspNetUserRoles/" + id, byteContent).Result;

                        if (postTask.IsSuccessStatusCode)
                        {
                            return(RedirectToAction("Index"));
                        }
                    }
                }
                catch (Exception)
                {
                    var aux2 = GetById(id);
                    if (aux2 == null)
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["RoleId"] = new SelectList(getAllAspNetRoles(), "Id", "Id", aspNetUserRoles.RoleId);
            ViewData["UserId"] = new SelectList(getAllAspNetUsers(), "Id", "Id", aspNetUserRoles.UserId);
            return(View(aspNetUserRoles));
        }
예제 #3
0
        public async Task <IActionResult> Create([Bind("Id,UserId,RoleId")] data.AspNetUserRoles aspNetUserRoles)
        {
            if (ModelState.IsValid)
            {
                using (var cl = new HttpClient())
                {
                    cl.BaseAddress = new Uri(baseurl);
                    var content     = JsonConvert.SerializeObject(aspNetUserRoles);
                    var buffer      = System.Text.Encoding.UTF8.GetBytes(content);
                    var byteContent = new ByteArrayContent(buffer);
                    byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                    var postTask = cl.PostAsync("api/AspNetUserRoles", byteContent).Result;

                    if (postTask.IsSuccessStatusCode)
                    {
                        return(RedirectToAction(nameof(Index)));
                    }
                }
            }
            ViewData["RoleId"] = new SelectList(getAllAspNetRoles(), "Id", "Id", aspNetUserRoles.RoleId);
            ViewData["UserId"] = new SelectList(getAllAspNetUsers(), "Id", "Id", aspNetUserRoles.UserId);
            return(View(aspNetUserRoles));
        }