c# - .NET Core 似乎将我的 POST 请求转换为 GET 请求
问题描述
我正在使用 Postman 和 Insomnia 向我的 .NET CORE 应用程序发送 POST 请求,并且我的[HttpPost]
方法中的断点被命中,但主体是空的,并且在调试 Visual Studio 时显示该请求是一个 GET 请求。
我的启动类:
namespace MyBuildingRestfulWebWithCore
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
//services.AddMvc();
services.AddControllers();
services.AddSingleton<IProductService, ProductService>();
services.AddDbContext<FlixOneStoreContext>(options => options.UseSqlServer("myconnectionstring;"));
services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
}
}
}
我的控制器:
namespace MyBuildingRestfulWebWithCore.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CustomersController : ControllerBase
{
private readonly FlixOneStoreContext _context;
public CustomersController(FlixOneStoreContext context)
{
_context = context;
}
[HttpGet] //this one works
public async Task<ActionResult<IEnumerable<Customer>>> GetCustomers()
{
return await _context.Customers.ToListAsync();
}
[HttpPost("test")] //this one fails because customers variable is null
public async Task<ActionResult<Customer>> PostCustomers([FromBody] Customer customers)
{
if (_context.Customers.Any(customer => customer.Email == customers.Email))
{
return StatusCode((int)HttpStatusCode.Conflict, "Email already in use");
}
_context.Customers.Add(customers);
//...
return CreatedAtAction("GetCustomers", new { id = customers.Id }, customers);
}
[HttpPost("test2")] //this one fails because request.Content is null,
//but Visual Studio debugger shows me that the request.Method is a GET request
public async Task<ActionResult<string>> PostCustomers(HttpRequestMessage request)
{
string body = await request.Content.ReadAsStringAsync();
return body;
}
}
我正在使用 .NET Core 3.0.0,并通过 Postman 和 Insomnia 发送我的请求以确认错误不在客户端 - 我已将 Postman 中的主体设置为 raw -JSON,将 Insomnia 中的主体设置为 JSON(以及我的 JSON验证)
我的请求正文如下:
{
"id":1,
"gender": "M",
"email":"test@test.com",
"firstname": "Firstname",
"lastname": "LastName",
"dob":"1970-07-05T00:00:0",
"mainaddressid":"mainaddr",
"fax":"fax",
"password": "pw",
"newsletteropted": false
}
我从官方文档中阅读了这篇关于如何迁移到 .NET Core 3 的文章,但没有发现任何帮助。
有任何想法吗?我是否错误地配置了我的启动类,或者使用了错误的某些属性?(我对 .NET Core 不太熟悉……)
解决方案
正如评论者对我的问题所暗示的那样,我的 JSON 不适合我的模型类,我只是没有意识到这一点,因为 HttpRequestMessage 在 aspnet 核心中不受支持并且其方法在调试器中显示为“GET”这一事实分散了我的注意力真正的问题,并且因为 SuppressModelStateInvalidFilter 抑制了错误,如上所述。
移除
services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
向我展示了实际的 JSON 解析错误并帮助我更正了我的输入。
推荐阅读
- html - 如何使用 prettier 处理 ">" 和 "{{" or ">" 和 "}}" 的换行符?
- c - 如何找到二维数组中所有列和行的总和
- android - 如何使用条件导航将 androidTest 添加到片段?
- javascript - 如何在 CRUD 表中实现过滤?
- django - django rest框架在更新字段时将图像字段设置为null
- typescript - 为什么没有关于通用接口错误实现的消息
- scala - Scala 没有这样的元素例外
- sql - 如何在 bigquery 数据表中添加计算列?
- python-3.x - 基于主列表自动排序文件
- java - 类数组的编译时初始化,其中包含一个整数数组