Reputation: 5058
Why can't I get to the controller in my asp.net core application. Please see code below.
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();
}
}
}
This is my class for model
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; }
}
My server is running so I try accessing it with postman like so:
{
"Username": "juan",
"FirstName": "Juan",
"MiddleName": "A.",
"LastName": "Dela Cruz",
"Password": "password",
"UserType": "User"
}
Server is running at
I use this url to post data
This is the error I get on postman. I am using the debug mode to get to my controller break point.
Upvotes: 1
Views: 111
Reputation: 36605
Firstly,as @Json's answer,you need to send request by url:https://localhost:44366/XXX
.
Then,For 400 Bad Request,you need to remove the following line:
[HttpPost]
//[ValidateAntiForgeryToken] //remove this line
[Route("add-user")]
Upvotes: 0
Reputation: 20092
@Jason answer could be solve your problem but I have this problem sometimes when using postman because I forgot to turn off the SSL certificate verification
Upvotes: 1
Reputation: 8887
You said the server is running at https://localhost:44366/ but you are sending requests to http://localhost:44366/
Upvotes: 0