Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions - Difference between return type HttpResponseMessage and IActionResult

I have created Http Trigger Azure Function and its default return type is Task<IActionResult>.

Is there any difference if I changed it to Task<HttpResponseMessage>?

Azure function with Task<IActionResult> return type:

 public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,

Azure function with Task<HttpResponseMessage>

 public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
            HttpRequestMessage message,
like image 389
Rakesh Kumar Avatar asked Sep 20 '25 17:09

Rakesh Kumar


1 Answers

Is there any difference if I changed Task<IActionResult> to Task<HttpResponseMessage>?

You can use the Task<HttpResponseMessage> to be the return type of your function. There shouldn't be a problem in executing with that.

Having said that, the difference between using the two lies in just the way you return response from your function.

  • In case of an IActionResult type of response, there is lesser code to write while constructing a response and it makes unit testing simpler.
  • On the other hand, HttpResponseMessage gives more control on the Http response message sent across the wire.

Just as a side note,

In HttpTrigger Azure function v1.0, types Task<HttpResponseMessage> and HttpRequestMessage were used as defaults for return type and request type respectively.

From v2.0 onwards, types Task<IActionResult> and HttpRequest are being used as default return type and request type respectively as it conforms to .net core API structure.

like image 172
akg179 Avatar answered Sep 22 '25 08:09

akg179