远程服务器返回ASP.Net Identity

时间:2016-10-27 09:53:28

标签: c# asp.net .net asp.net-mvc

我正在为ASP.net编写一个应用程序,其中超级管理员用户可以添加Roles.I有一个单独的服务控制器,用于处理创建用户和检索所有用户。我的UI项目中有另一个控制器,它将获取服务URI并将数据发送到服务。然后从那里到数据库。我不断收到错误“远程服务器返回错误(405)方法不允许”。由于我是ASP.net的新手,如果有人可以告诉我哪里出错了,那么Identity会要求。

UI项目控制器

[HttpPost, Route("/Role/createNewMember")]
        public PartialViewResult createNewMember([FromBody]Role Model)
        {
            int finalIndex = (RolesViewModel.Roles.Count) - 1;
            Model.RoleID = RolesViewModel.Roles[finalIndex].RoleID + 1;

            try
            {

                string roleData = JsonConvert.SerializeObject(Model);

                this.PostAsync("http://localhost:8533/api/Roles/", roleData);
                RolesViewModel.Roles.Add(Model);
            }
            catch (AggregateException e)
            {
            }

            return PartialView("_RoleModel", RolesViewModel);

        }


        public void PostAsync(string uri, string data)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";

            request.ContentType = "application/json";

            using (var sw = new StreamWriter(request.GetRequestStream()))
            {
                sw.Write(data);
                sw.Flush();
            }


            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        }

休息服务类

  [Authorize(Roles="Superadmin")]
    [RoutePrefix("api/roles")]
    public class RolesController : ApiController
    {
        public UserManager<ApplicationUser> UserManager { get; private set; }
        public RoleManager<IdentityRole> RoleManager { get; private set; }
        public AuthContext context { get; private set; }

        public RolesController()
        {
            context = new AuthContext();
            UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
        }

        public RolesController(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
        {
            UserManager = userManager;
            RoleManager = roleManager;
        }
 // POST: api/roles
        [Route("Post")]
        [HttpPost]
        public async Task<IHttpActionResult> Post(RoleBindingModels roleViewModel)
        {
            if (ModelState.IsValid)
            {
                var role = new IdentityRole(roleViewModel.Name);
                var roleresult = await RoleManager.CreateAsync(role);
                if (!roleresult.Succeeded)
                {
                    ModelState.AddModelError("", roleresult.Errors.First().ToString());

                }
                return BadRequest(ModelState);
            }
            return CreatedAtRoute("DefaultApi", new { id = 100 }, roleViewModel);

        }

0 个答案:

没有答案