Azure Function return type

Anwar Hamida 1 Reputation point
2022-03-26T19:57:13.227+00:00

187060-immagine-2022-03-25-120036screen.png

Hi ,
I wanted to create an http triggered Azure function , so that once i call /api/myfunction it download a file/image. But i can't figure out what should be the return type of the function.

What should i put instead of "HttpResponseData" in the Screenshot.
Thanks.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
Developer technologies ASP.NET ASP.NET Core
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-03-28T06:07:05.807+00:00

    Hi @Anwar Hamida

    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

    187392-image.png

    Test Result

    fIt1avm.gif


    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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.