18
votes

I'm working on an Azure Function App (v2), which has a number of different functions inside the same Function App.

It is going to be deployed in multiple Azure regions, and I would like to add a custom header, that indicates which region served the request.

I know that I can let each function return HttpResponseMessage which can contain custom headers. Instead of duplicating that code to each function, I would like to make it centralized in the entire Function App project. Another drawback of returning HttpResponseMessage is that I would like to use IActionResult, so I can use JsonResult or OkObjectResult and similar return types.

With an ASP.NET MVC project, I could add a middleware which added the header or add the headers using web.config. Neither seems to be possible in a Function App.

How can this be done without having to return HttpResponseMessage and adding the header inside each function's Run method?

1
Hve a look at function filter, it should fit your requirements: github.com/Azure/azure-webjobs-sdk/wiki/Function-Filters.Thomas
Thank you for the pointer. Function filters are close to what I want. At least they are injected into the Function's execution flow. But it seems the feature isn't done yet (github.com/Azure/azure-webjobs-sdk/issues/1284) and they can't modify the function's response (github.com/Azure/azure-webjobs-sdk/issues/1314)RasmusW

1 Answers

26
votes

I was able to do this by accessing the HttpResponse object via the request's HttpContext.

For example:

req.HttpContext.Response.Headers.Add("ETag", entity.ETag);
return new OkObjectResult(entity);

produces: enter image description here