The template for the code in your screenshot looks like it was created in vscode. In fact, you can also use Visual Studio 2022 to create Azure Function. After creation, you will find that the project structure is basically similar to that of webapp.
In my sample code, you will find that I refer to the official documentation, use a Startup.cs file similar to that in webapp, and inject HttpClient as a dependency, so that it can be used in Function Class. The programming habits are basically the same as those of webapp. This is why I recommend using Visual Studio.
Official Doc: Use dependency injection in .NET Azure Functions
My test code for you, you can use FileContentResult
in this thread.
Function1.cs
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.Http;
namespace FunctionAppAnonymous
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
[FunctionName("Function2")]
public static async Task<IActionResult> TestAnonymousMP4(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
string url = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
using (var client = new HttpClient())
{
using (var result = await client.GetAsync(url))
{
if (result.IsSuccessStatusCode)
{
return new FileContentResult(await result.Content.ReadAsByteArrayAsync(), "video/mp4")
{
FileDownloadName = Guid.NewGuid() + ".mp4"
};
}
}
}
return null;
}
[FunctionName("Function3")]
public static async Task<IActionResult> TestAnonymousJPG(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
string url = "https://raw.githubusercontent.com/snorpey/jpg-glitch/master/images/screenshot.png";
using (var client = new HttpClient())
{
using (var result = await client.GetAsync(url))
{
if (result.IsSuccessStatusCode)
{
return new FileContentResult(await result.Content.ReadAsByteArrayAsync(), "image/png")
{
FileDownloadName = Guid.NewGuid() + ".png"
};
}
}
}
return null;
}
}
}
Startup.cs
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(FunctionAppAnonymous.Startup))]
namespace FunctionAppAnonymous
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient();
}
}
}
Image of project structure
Test Result
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
Jason