Nis*_*nga 21 c# .net-core asp.net-core-webapi asp.net-core-2.1
我创建了一个用户注册控制器来注册具有存储库设计模式的用户.我的控制器看起来像这样.
[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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用Postman发送请求时,它会向我提供404未找到的状态代码,并且API会在不读取整个正文的情况下报告请求已完成.
我已经使用数据传输对象(DTO)封装的数据,我删除了UserForRegisterDto
,并试图使用string username
和string password
,如下所示但没有奏效.
public async Task<IActionResult> Register([FromBody] string username, string password)
Run Code Online (Sandbox Code Playgroud)
UserForRegisterDto
看起来像这样.
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; }
}
Run Code Online (Sandbox Code Playgroud)
我为此尝试了许多在线解决方案,但到目前为止还没有解决我的问题.请帮我解决问题,谢谢你提前.我在Ubuntu 18.04上运行这个API
编辑: 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();
}
}
Run Code Online (Sandbox Code Playgroud)
itm*_*nus 20
the application completed without reading the entire request body
当客户端发送不满足服务器要求的请求时,通常会发生错误信息.换句话说,它恰好在进入操作之前发生,导致您无法通过操作体方法中的断点对其进行调试.
例如,假设服务器上有一个action方法:
[Route("api/[controller]")]
[ApiController]
public class DummyController : ControllerBase
{
[HttpPost]
public DummyDto PostTest([FromBody] DummyDto dto)
{
return dto;
}
}
Run Code Online (Sandbox Code Playgroud)
这DummyDto
是一个用于保存信息的虚拟类:
public class DummyDto
{
public int Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当客户端发送请求时,有效负载格式不正确
例如,以下发布请求,没有Content-Type: application/json
标题:
POST https://localhost:44306/api/test HTTP/1.1
Accept : application/json
{ "id":5 }
Run Code Online (Sandbox Code Playgroud)
将导致类似的错误信息:
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 POST http://localhost:44306/api/test 10
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 1.9319ms 404
Microsoft.AspNetCore.Server.Kestrel:Information: Connection id "0HLGH8R93RPUO", Request id "0HLGH8R93RPUO:00000002": the application completed without reading the entire request body.
Run Code Online (Sandbox Code Playgroud)
并且来自服务器的响应将是404
:
HTTP/1.1 404 Not Found
Server: Kestrel
X-SourceFiles: =?UTF-8?B?RDpccmVwb3J0XDIwMThcOVw5LTFcU08uQXV0aFJlYWRpbmdXaXRob3V0RW50aXRlQm9keVxBcHBcQXBwXGFwaVx0ZXN0?=
X-Powered-By: ASP.NET
Date: Mon, 03 Sep 2018 02:42:53 GMT
Content-Length: 0
Run Code Online (Sandbox Code Playgroud)
至于你描述的问题,我建议你检查以下列表:
Content-Type: application/json
?的请求?确保您已检查标题code
以显示向服务器发送请求时确切发送的内容.Vil*_*mir 16
在本地进行调试时,在新的ASP.NET Core 2.1服务中发生了这种情况,因为我在Startup.Configure中进行了以下操作:
app.UseHttpsRedirection();
Run Code Online (Sandbox Code Playgroud)
我在本地调试时停用了此设置:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHttpsRedirection();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15784 次 |
最近记录: |