简体   繁体   中英

Unable to create HttpGet and HttpDelete in asp dotnet core web api

I'm new to asp dotnet and trying to create web API to store the data from the clientside and I was able to create HttpPost but unable to create HttpGet And HttpDelete endpoint. Normally, I used to do by mapping but here I have used aspNet core identity to try new idea but couldn't make it work, could anyone help me please, here is my code:

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using WebApi.Data;
using WebApi.User;

namespace WebApi.Controllers
{
    [ApiController]
    [Route("api/Application/")]

    public class ApplicationUserController : ControllerBase
    {
        private UserManager<ApplicationUser> userManager;
        private SignInManager<ApplicationUser> signInManager;
        private readonly DataContext dataContext;
        public ApplicationUserController(UserManager<ApplicationUser> userManager, DataContext dataContext, SignInManager<ApplicationUser> signInManager)
        {
            this.userManager = userManager;
            this.signInManager = signInManager;
            this.dataContext = dataContext;
        }

        [HttpPost]
        [Route("register")]
        public async Task<Object>CreateUser(ApplicationUserDTO user)
        {
            user.Role = "Member";
            var applicationUser = new ApplicationUser()
            {
                UserName = user.UserName,
                FullName = user.FullName,
                PhoneNumber = user.PhoneNumber,
                Email = user.Email
            };
            try
            {
                var result = await userManager.CreateAsync(applicationUser, user.Password);
                await userManager.AddToRoleAsync(applicationUser, user.Role);
                return Ok(result);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
       //To Do
       // create Httpget and HttpDelete
    }
}

ApplicationUser.cs

using Microsoft.AspNetCore.Identity;

namespace WebApi.User
{
    public class ApplicationUser:IdentityUser
    {
        public string FullName { get; set; }

    }
}

ApplicationUserDTO.cs

using System;

namespace WebApi.User
{
    public class ApplicationUserDTO
    {

        public Guid Id { get; set; }
        public string UserName { get; set; }
        public string FullName { get; set; }
        public string PhoneNumber { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Role { get; set; }


    }
}

thank you !!

I tried to do mapping that's why I got the error I've fixed it and here is my HttpGet: method.

[HttpGet("{id}")]
        public async Task<ActionResult<ApplicationUser>> GetById(string id)
        {
            var todoItem = await dataContext.ApplicationUsers.FindAsync(id);

            if (todoItem == null)
            {
                return NotFound();
            }

            return todoItem;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM