You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决ASP.NET Core 8 MVC控制器无法返回JSON的问题

ASP.NET Core 8 MVC返回JSON异常问题分析

问题现象

在ASP.NET Core 8 MVC项目中,HttpPost控制器方法返回Ok(new { Test = "Value" })或Created()时无响应,但返回BadRequest()能正常返回JSON:

[HttpPost]
public IActionResult Test()
{
    return Ok(new { Test = "Value" }); // 无响应
    // return Created("", new { Test = "Value" }); // 同样无响应
    // return BadRequest(new { Test = "Value" }); // 正常返回JSON
}

项目Program.cs配置如下:

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using RisingTide.Center.Data;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<RisingTideCenterContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("RisingTideCenterContext") ?? throw new InvalidOperationException("Connection string 'RisingTideCenterContext' not found.")));

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

问题原因

ASP.NET Core MVC模板(通过AddControllersWithViews()注册服务)的默认行为是:对于成功状态码的响应(如200 OK、201 Created),会优先尝试匹配并返回对应视图文件,当找不到视图时会陷入等待或返回空响应;而错误状态码的响应(如400 BadRequest)则默认跳过视图匹配,直接返回JSON内容。

你的代码中返回Ok()或Created()时,框架会尝试寻找/Views/[控制器名]/Test.cshtml视图文件,找不到的情况下就会出现无响应的情况,而BadRequest()直接返回JSON,所以能正常显示。

解决方案

可以通过以下几种方式解决:

1. 直接返回JsonResult

在控制器方法中显式返回JsonResult,强制指定返回JSON格式:

[HttpPost]
public IActionResult Test()
{
    return Json(new { Test = "Value" });
}

2. 修改MVC服务配置,默认返回JSON

注册AddControllersWithViews()时,调整输出格式化规则,将JSON设为优先响应格式:

builder.Services.AddControllersWithViews(options =>
{
    // 当客户端请求格式不被支持时,返回406 Not Acceptable
    options.ReturnHttpNotAcceptable = true;
    // 将JSON格式化器移到输出格式列表的最前面
    var jsonFormatter = options.OutputFormatters.OfType<SystemTextJsonOutputFormatter>().First();
    options.OutputFormatters.Remove(jsonFormatter);
    options.OutputFormatters.Insert(0, jsonFormatter);
})
.AddJsonOptions(options =>
{
    // 可按需配置JSON序列化规则,比如保留原属性名
    // options.JsonSerializerOptions.PropertyNamingPolicy = null;
});

3. 请求时指定Accept头

发起HttpPost请求时,设置请求头Accept: application/json,明确告知服务器期望接收JSON格式响应,框架会直接返回JSON而不尝试匹配视图。


内容的提问来源于stack exchange,提问作者Anthony Mallgren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 10:44:58