Create Your First Azure Function with Visual Studio

This guide will walk you through the process of creating your very first Azure Function using Visual Studio. Azure Functions provide a serverless compute experience that you can use to build and deploy event-driven solutions on Azure. We'll create a simple HTTP-triggered function.

Prerequisites

Steps

1

Open Visual Studio and create a new project.

2

In the "Create a new project" dialog, search for Azure Functions and select the Azure Functions template.

Click Next.

3

Configure your new project:

  • File name: Enter a name for your project, e.g., MyAwesomeFunctions.
  • Location: Choose a directory to save your project.

Click Create.

4

Configure your Azure Functions project:

  • Function: Select HTTP trigger.
  • Authorization level: Choose Anonymous for simplicity during development.

Click Create.

5

Visual Studio creates your project and a default HttpTrigger1.cs (or similar) file. This file contains the code for your HTTP-triggered function.

Examine the code. You'll see attributes defining the trigger and the function's entry point. The function typically takes an HttpRequest object and returns an HttpResponseMessage.

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;

namespace MyAwesomeFunctions
{
    public static class HttpTrigger1
    {
        [FunctionName("HttpTrigger1")]
        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);
        }
    }
}
6

Press F5 or click the Start button in Visual Studio to run your Azure Functions project locally.

The Azure Functions Core Tools will start, and your function will be hosted locally. You'll see output in the terminal indicating the local URL for your HTTP trigger.

7

Open a web browser and navigate to the URL provided in the terminal. It will typically look something like this:

http://localhost:7071/api/HttpTrigger1

Since we set the authorization level to Anonymous, you can access it directly. You should see the default response message.

Try adding a name parameter to the URL:

http://localhost:7071/api/HttpTrigger1?name=AzureUser

You should now see a personalized greeting.

Next Steps

Congratulations! You've created and run your first Azure Function. From here, you can:

Note: When deploying to Azure, you'll typically use a higher authorization level like Function or Admin and manage function keys. The Anonymous level is mainly for easy local testing.

Resources

Azure Functions Documentation Azure Functions .NET Samples