无法到达 asp.net 核心中的路线
Could not get to a route in asp.net core
为什么我无法访问 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();
}
}
}
这是我的class模特
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; }
}
我的服务器是 运行 所以我尝试像这样用 postman 访问它:
{
"Username": "juan",
"FirstName": "Juan",
"MiddleName": "A.",
"LastName": "Dela Cruz",
"Password": "password",
"UserType": "User"
}
服务器 运行
我用这个url到post数据
这是我在 postman 上遇到的错误。我正在使用调试模式到达我的控制器断点。
你说服务器是 运行 在 https://localhost:44366/ 但是你发送请求到 http://localhost:44366/
@Jason 的回答可以解决你的问题,但我在使用邮递员时有时会遇到这个问题,因为我忘记关闭 SSL certificate verification
首先,作为@Json的回答,您需要通过url发送请求:https://localhost:44366/XXX
。
然后,对于 400 Bad Request,您需要删除以下行:
[HttpPost]
//[ValidateAntiForgeryToken] //remove this line
[Route("add-user")]
为什么我无法访问 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();
}
}
}
这是我的class模特
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; }
}
我的服务器是 运行 所以我尝试像这样用 postman 访问它:
{
"Username": "juan",
"FirstName": "Juan",
"MiddleName": "A.",
"LastName": "Dela Cruz",
"Password": "password",
"UserType": "User"
}
服务器 运行
我用这个url到post数据
这是我在 postman 上遇到的错误。我正在使用调试模式到达我的控制器断点。
你说服务器是 运行 在 https://localhost:44366/ 但是你发送请求到 http://localhost:44366/
@Jason 的回答可以解决你的问题,但我在使用邮递员时有时会遇到这个问题,因为我忘记关闭 SSL certificate verification
首先,作为@Json的回答,您需要通过url发送请求:https://localhost:44366/XXX
。
然后,对于 400 Bad Request,您需要删除以下行:
[HttpPost]
//[ValidateAntiForgeryToken] //remove this line
[Route("add-user")]