无法在asp dotnet core web api中创建HttpGet和HttpDelete。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法在asp dotnet core web api中创建HttpGet和HttpDelete。相关的知识,希望对你有一定的参考价值。

我是一个新的asp dotnet,并试图创建Web API从客户端存储数据,我能够创建HttpPost,但无法创建HttpGet和HttpDelete端点。通常情况下,我都是通过映射来实现的,但在这里我使用了aspNet核心身份来尝试新的想法,但无法使其工作,请任何人帮助我,这是我的代码。

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; 


    

谢谢你!

答案

我试图做映射,这就是为什么我得到的错误,我已经修复了它,这是我的HttpGet :方法。

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

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

            return todoItem;
        

以上是关于无法在asp dotnet core web api中创建HttpGet和HttpDelete。的主要内容,如果未能解决你的问题,请参考以下文章

从 ASP.NET MVC 5 移植到 dotnet core 2:可能替代 System.Web.Mvc.HttpStatusCodeResult [重复]

ASP.NET Core Web Api配置Azure Key Vault

dotNet Core初学之创建第一个dotNetCore项目

dotnet core 2.0中appSettings.json中的SMTP设置

如何为 dotnet core(web Api) 代码更改以及 TypeScript 代码更改启用实时重新加载

asp.net core-项目开发中问题汇总