无法到达asp.net核心中的路由

2020-02-15 c# asp.net-core .net-core asp.net-core-mvc asp.net-core-routing

为什么我无法在asp.net核心应用程序中找到控制器。请参见下面的代码。

using System;
using System.Threading.Tasks;
using HashingApplication;
using Microsoft.AspNetCore.Mvc;
using NgWithJwt.Models;

namespace NgWithJwt.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly ApplicationDbContext context;

        public UserController(ApplicationDbContext context)
        {
            this.context = context;
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Route("add-user")]
        public async Task<IActionResult> AddUser(AppUser model)
        {
            if (ModelState.IsValid)
            {
                var saltValue = Guid.NewGuid().ToString() + model.Password;
                var password = Encryption.Encrypt(model.Password, saltValue);

                model.SaltValue = saltValue;
                model.Password = password;
                model.CreatedDate = DateTime.Now;

                context.Add(model);
                var response = await context.SaveChangesAsync();
                if(response > 0)
                {
                    return Ok(model);
                }
            }
            return BadRequest();
        }
    }
}

这是我的模特班

 public partial class AppUser
    {
        [Key]
        public int Id { get; set; }

        [Column(TypeName = "nvarchar(100)")]
        [Required(ErrorMessage = "Username is required.")]
        [DisplayName("Username")]
        public string UserName { get; set; }

        [Column(TypeName = "nvarchar(100)")]
        [Required(ErrorMessage = "First Name is required")]
        [DisplayName("First Name")]
        public string FirstName { get; set; }

        [Column(TypeName = "nvarchar(100)")]
        [DisplayName("Middle Name")]
        public string MiddleName { get; set; }

        [Column(TypeName = "nvarchar(100)")]
        [Required(ErrorMessage = "Last Name is required")]
        [DisplayName("Last Name")]
        public string LastName { get; set; }

        [Column(TypeName = "nvarchar(max)")]
        public string SaltValue { get; set; }

        [Column(TypeName = "nvarchar(100)")]
        [Required(ErrorMessage = "Password is required")]
        [DisplayName("Password")]
        public string Password { get; set; }

        [Column(TypeName = "nvarchar(100)")]
        public string UserType { get; set; }
        public DateTime CreatedDate { get; set; }
    }

我的服务器正在运行,因此我尝试像这样用邮递员访问它:

{
    "Username": "juan",
    "FirstName": "Juan",
    "MiddleName": "A.",
    "LastName": "Dela Cruz",
    "Password": "password",
    "UserType": "User"
}

服务器正在运行

https:// localhost:44366 /

我使用此网址发布数据

http:// localhost:44366 / api / user / add-user

这是我遇到邮递员的错误。我正在使用调试模式到达控制器断点。

在此处输入图片说明

Answers

您说服务器正在https :// localhost:44366 /上运行,但是您正在向http :// localhost:44366 /发送请求

@Jason的答案可以解决您的问题,但使用邮递员时有时会出现此问题,因为我忘记了关闭SSL certificate verification

在此处输入图片说明

首先,作为@Json的答案,您需要通过url发送请求: https://localhost:44366/XXX

然后,对于“ 400错误请求”,您需要删除以下行:

[HttpPost]
//[ValidateAntiForgeryToken]   //remove this line
[Route("add-user")]

Related