I am using a body in a http DELETE
request. I know having a body in a delete is non-standard at the moment (but is permissible).
The problem is occurring when using the HttpClient
which does not allow a body for delete requests. I know I can just use SendAsync
, but I would rather make my API more flexible.
I want this body to be optional. In the sense that if asp.net core cannot ascertain the content type then ignore it. At the moment, asp.net core is returning a 415, even though no body is being sent (via the HttpClient
- so content length should be 0).
Can FromBody
be extended in this way? Or would I need some custom logic in the pipeline?
You can create ResourceFilter which is executed before Model Binding where content type is checked:
public class AddMissingContentType : Attribute, IResourceFilter
{
public void OnResourceExecuting(ResourceExecutingContext context)
{
context.HttpContext.Request.Headers["Content-Type"] = "application/json";
}
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
}
And add it to your method:
[AddMissingContentType]
[HttpDelete]
public async Task<IActionResult> Delete([FromBody]RequestData request)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With