admin管理员组文章数量:1241128
I'm trying to add HTMX to a project that uses Razor Pages and MVC controllers.
I have a form submitted by HTMX. It's handled by an MVC action, which returns a status code and empty response.
[Controller]
public class CustomerController : Controller
{
[HttpPost]
public async Task<IActionResult> CreateCustomer(CustomerDto dto)
{
// errors: return other status codes...
// success: return 204 for HTMX (not 200)
return NoContent();
}
}
The correct status code is returned, so the HTMX event handlers take appropriate action.
However the response always includes a body; specifically, 400...599 error pages. This particular action does not render a view, so I don't understand why it's happening.
What is the problem, and how do I fix it?
(ASP.NET Core v8)
I'm trying to add HTMX to a project that uses Razor Pages and MVC controllers.
I have a form submitted by HTMX. It's handled by an MVC action, which returns a status code and empty response.
[Controller]
public class CustomerController : Controller
{
[HttpPost]
public async Task<IActionResult> CreateCustomer(CustomerDto dto)
{
// errors: return other status codes...
// success: return 204 for HTMX (not 200)
return NoContent();
}
}
The correct status code is returned, so the HTMX event handlers take appropriate action.
However the response always includes a body; specifically, 400...599 error pages. This particular action does not render a view, so I don't understand why it's happening.
What is the problem, and how do I fix it?
(ASP.NET Core v8)
Share Improve this question edited yesterday marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked yesterday lonixlonix 20.9k29 gold badges131 silver badges274 bronze badges2 Answers
Reset to default 1That the response contains an error page was the hint I needed to resolve the issue.
I realised the configuration included the Status Code Pages middleware, with the UseStatusCodePagesWithReExecute
option. So when returning a status code in the 400...599 range, the middleware would automatically re-execute the request and show a standard error page. One doesn't inspect the configuration often, so this was easy to fet / overlook.
I haven't read that docs page in years, but upon rereading it discovered that there's a nice feature to skip the middleware. The easiest is with an attribute on a Razor Pages page handler or MVC action. So in this case:
[SkipStatusCodePages]
[HttpPost]
public async Task<IActionResult> CreateCustomer(CustomerDto dto) { }
Now it returns the status code and an empty response, and HTMX can handle the rest.
-->You May write in try catch block and also verify dto not empty
public async Task<IActionResult> CreateCustomer(CustomerDto dto)
{
try{
if(dto==null)
{
return BadRequest(new{message="Invali request data!!"});
}
return NoContent();
}
catch(Exception e)
{
return StatusCode(500, new { message = "An unexpected error
occurred.", details = e.Message });
}
}
本文标签: Return empty response from ASPNET Core for HTMXStack Overflow
版权声明:本文标题:Return empty response from ASP.NET Core for HTMX - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740037752a2221548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论