【问题标题】:Could not get to a route in asp.net core无法到达 asp.net 核心中的路由
【发布时间】:2020-05-30 19:07:21
【问题描述】:

为什么我无法访问我的 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

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

【问题讨论】:

  • 您好,我的回答对您有帮助吗?

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


【解决方案1】:

@Jason 的回答可能会解决你的问题,但我在使用邮递员时有时会遇到这个问题,因为我忘记关闭 SSL certificate verification

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这是我使用的第一个网址。但是当我运行它时,它给了我一个错误 "Bad Request 400" { "type": "tools.ietf.org/html/rfc7231#section-6.5.1", "title": "Bad Request", "status": 400, "traceId": "|5e9a7fb9- 4585aa9d52ce1629。” }
    【解决方案3】:

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

    然后,对于 400 Bad Request,您需要删除以下行:

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

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      相关资源
      最近更新 更多