I have created a user register controller to register users with repository design pattern. My controller looks like this.
[Route("api/[controller]")]
public class AuthController : Controller
{
private readonly IAuthRepository _repo;
public AuthController(IAuthRepository repo)
{
_repo = repo;
}
[AllowAnonymous]
[HttpPost("register")]
public async Task<IActionResult> Register([FromBody] UserForRegisterDto userForRegisterDto){
// validate request
if(!ModelState.IsValid)
return BadRequest(ModelState);
userForRegisterDto.Username = userForRegisterDto.Username.ToLower();
if(await _repo.UserExists(userForRegisterDto.Username))
return BadRequest("Username is already taken");
var userToCreate = new User{
Username = userForRegisterDto.Username
};
var createUser = await _repo.Register(userToCreate, userForRegisterDto.Password);
return StatusCode(201);
}
}
When I send a request using Postman, it gives me the the 404 not found status code, and API reports the request completed without reading the entire body.
My request in Postman looks like this.
I have used Data Transfer Objects(DTO) to encapsulate data, I removed UserForRegisterDto
and tried to use string username
and string password
, as follows but it did not work.
public async Task<IActionResult> Register([FromBody] string username, string password)
UserForRegisterDto
looks like this.
public class UserForRegisterDto
{
[Required]
public string Username { get; set; }
[Required]
[StringLength(8, MinimumLength =4, ErrorMessage = "You must specify a password between 4 and 8 characters.")]
public string Password { get; set; }
}
I have tried many online solutions for this, but so far nothing resolved my problem. Please help me to troubleshoot the issue, Thank you in advance. I'm running this API on Ubuntu 18.04
Edit: Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>(x => x.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddCors();
services.AddScoped<IAuthRepository, AuthRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
app.UseMvc();
}
}
i have same error, check maybe you put (AutoValidateAntiforgeryTokenAttribute) in AddMvc Services
It happened to me in a new ASP.NET Core 2.1 service when debugging in localhost because I had in Startup.Configure:
I deactivated this setting when debugging locally:
The error info of
the application completed without reading the entire request body
often occurs when the client send a request that doesn't fulfill the sever requirements . In other words , it happens just before entering the action , resulting that you cannot debug it via a breakpoint within the body of action method .For example , let's say a action method on the server :
The
DummyDto
here is a dummy class to hold information:When clients send a request with payload not well formatted
For example , the following post request , which doesn't have a
Content-Type: application/json
header :will result in a similar error info :
and the response from the server will be
404
:As for the question you described , I suggest you should check the following list :
Content-Type: application/json
? make sure you have checked the headercode
to show what it sends exactly when you send a request to the server .In my case, was the query wrong:
Solved after fix
&&
wrongAND
okHad the same issue running a
Dotnet 2.2
andNGinx
on remoteUbuntu 18.04
-machine, got a:on every API-call. What solved it for me was to install the SSL Certificates on the host through CERTBot from Let's encrypt, since
Dotnet
doesn't allow un-encrypted traffic.Hopes this helps someone
There can be multiple reasons out of which one can be : – Caching in Visual Studio --