The application completed without reading the entire request body, .net core 2.1.1

Nishān Wickramarathna picture Nishān Wickramarathna · Sep 1, 2018 · Viewed 21.8k times · Source

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.

enter image description here

My request in Postman looks like this. enter image description here

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();
        }
    }

Answer

Vilmir picture Vilmir · Mar 20, 2019

It happened to me in a new ASP.NET Core 2.1 service when debugging in localhost because I had in Startup.Configure:

app.UseHttpsRedirection();

I deactivated this setting when debugging locally:

if (env.IsDevelopment())
{
     app.UseDeveloperExceptionPage();
}
else
{
     app.UseHttpsRedirection();
}