最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Return empty response from ASP.NET Core for HTMX - Stack Overflow

programmeradmin4浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

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

发布评论

评论列表(0)

  1. 暂无评论