热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:应用程序在没有读取整个请求主体的情况下完成,.netcore2.1.1

篇首语:本文由编程笔记#小编为大家整理,主要介绍了应用程序在没有读取整个请求主体的情况下完成,.netcore2.1.1相关的知识,希望对你有一定的参考价值。

篇首语:本文由编程笔记#小编为大家整理,主要介绍了应用程序在没有读取整个请求主体的情况下完成,.net core 2.1.1相关的知识,希望对你有一定的参考价值。



我创建了一个用户注册控制器来注册用户的存储库设计模式。我的控制器看起来像这样。

[Route("api/[controller]")]
public class AuthController : Controller
{
private readonly IAuthRepository _repo;
public AuthController(IAuthRepository repo)
{
_repo = repo;
}
[AllowAnonymous]
[HttpPost("register")]
public async Task 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);
}
}

当我使用Postman发送请求时,它会向我提供404未找到的状态代码,并且API会在不读取整个正文的情况下报告请求已完成。

enter image description here

我在Postman的请求看起来像这样。 enter image description here

我已经使用数据传输对象(DTO)来封装数据,我删除了UserForRegisterDto并尝试使用string usernamestring password,如下所示,但它不起作用。

public async Task Register([FromBody] string username, string password)

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

我为此尝试了许多在线解决方案,但到目前为止还没有解决我的问题。请帮我解决问题,谢谢你提前。我在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(x => x.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddCors();
services.AddScoped();
}
// 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();
}
}

答案

当客户端发送不满足服务器要求的请求时,经常会出现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;
}
}

这里的DummyDto是一个用于保存信息的虚拟类:

public class DummyDto
{
public int Id { get; set; }
}

当客户端发送请求时,有效负载格式不正确

例如,以下发布请求,其中没有Content-Type: application/json标头:

POST https://localhost:44306/api/test HTTP/1.1
Accept : application/json
{ "id":5 }

将导致类似的错误信息:

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.

并且来自服务器的响应将是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

至于你描述的问题,我建议你检查以下列表:

enter image description here



  1. 邮递员是否发送了带有Content-Type: application/json标题的请求?确保您已检查标题

  2. 如果step1不起作用,请单击code以显示向服务器发送请求时的确切发送内容。


另一答案

在localhost中进行调试时,我遇到了一个新的ASP.NET Core 2.1服务,因为我在Startup.Configure中:

app.UseHttpsRedirection();

我在本地调试时停用了此设置:

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

另一答案

可能有多种原因可以解决: - 在Visual Studio中缓存 -

1.Close all the instances of visual studios, run Developer command prompt with Admin rights.
2.git clean -xfd [Your Repository to remove all dependencies and existing soln file]
3.take the latest build and run . [Make Endpoint AllowAnonymous]

另一答案

我有同样的错误(即使使用“Content-Type:application / json”),但在动作动词中添加“{id}”,即为

[HttpPatch]
[ActionName("Index")]
[Authorize(Policy = "Model")]
public async Task Update([FromRoute]int id, int modelId, [FromBody]Device device)

[HttpPatch("{id}")]
[ActionName("Index")]
[Authorize(Policy = "Model")]
public async Task Update([FromRoute]int id, int modelId, [FromBody]Device device)

(asp.net核心2.1)


另一答案

你可以通过添加请求方法[Route(“jsonbody”)]来尝试

[AllowAnonymous]
[HttpPost("register")]
[Route("jsonbody")]
public async Task Register([FromBody] UserForRegisterDto userForRegisterDto){}

另一答案

我有相同的错误,检查你可能在AddMvc服务中放入(AutoValidateAntiforgeryTokenAttribute)

services.AddMvc(opt => {
//Prevent CSF Attake For POST,PUT,DELETE Verb
//opt.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
})

另一答案

在我的情况下,查询错误:

SELECT * FROM dbo.person WHERE login= 'value' && pass = 'value'

解决了修复&&错误的AND确定

SELECT * FROM dbo.person WHERE login= 'value' AND pass = 'value'


推荐阅读
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 有没有一种方法可以在不继承UIAlertController的子类或不涉及UIAlertActions的情况下 ... [详细]
  • 如何在服务器主机上实现文件共享的方法和工具
    本文介绍了在服务器主机上实现文件共享的方法和工具,包括Linux主机和Windows主机的文件传输方式,Web运维和FTP/SFTP客户端运维两种方式,以及使用WinSCP工具将文件上传至Linux云服务器的操作方法。此外,还介绍了在迁移过程中需要安装迁移Agent并输入目的端服务器所在华为云的AK/SK,以及主机迁移服务会收集的源端服务器信息。 ... [详细]
  • flowable工作流 流程变量_信也科技工作流平台的技术实践
    1背景随着公司业务发展及内部业务流程诉求的增长,目前信息化系统不能够很好满足期望,主要体现如下:目前OA流程引擎无法满足企业特定业务流程需求,且移动端体 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • 本文介绍了ASP.NET Core MVC的入门及基础使用教程,根据微软的文档学习,建议阅读英文文档以便更好理解,微软的工具化使用方便且开发速度快。通过vs2017新建项目,可以创建一个基础的ASP.NET网站,也可以实现动态网站开发。ASP.NET MVC框架及其工具简化了开发过程,包括建立业务的数据模型和控制器等步骤。 ... [详细]
  • 在本教程中,我们将看到如何使用FLASK制作第一个用于机器学习模型的RESTAPI。我们将从创建机器学习模型开始。然后,我们将看到使用Flask创建AP ... [详细]
  • Django + Ansible 主机管理(有源码)
    本文给大家介绍如何利用DjangoAnsible进行Web项目管理。Django介绍一个可以使Web开发工作愉快并且高效的Web开发框架,能够以最小的代价构建和维护高 ... [详细]
  • packagetzy.template.controller;importorg.apache.shiro.authc.ExpiredCredentialsException;im ... [详细]
  • 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之六 || API项目整体搭建 6.1 仓储模式
    代码已上传Github+Gitee,文末有地址  书接上文:前几回文章中,我们花了三天的时间简单了解了下接口文档Swagger框架,已经完全解放了我们的以前的Word说明文档,并且可以在线进行调 ... [详细]
  • Spring Boot 中 Java8 LocalDateTime 序列化问题
    LoginController页面如下:publicObjectlogin(@RequestBodyUseruser){returnxxxx ... [详细]
  • 本文整理了Java中org.assertj.core.api.AbstractPathAssert.existsNoFollowLinks()方法的一些代码示例,展示了 ... [详细]
  • springboot基于redis配置session共享项目环境配置pom.xml引入依赖application.properties配置Cookie序列化(高版本不需要)测试启 ... [详细]
author-avatar
xinlang138438
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有