Getting Started with Azure Functions HTTP Trigger using C# .NET

Dhanesh Kumar MJ
2 min readOct 12

--

Welcome to this beginner-friendly guide on creating an Azure Function with an HTTP Trigger using C#.NET. In just a few steps, you’ll have your own serverless API up and running. Let’s dive in!

Azure Function

Prerequisites

Before we start, make sure you have the following:

Step 1: Create an Azure Functions Project

  1. Open your terminal and navigate to your preferred project directory.
  2. Run this command to create a new Azure Functions project
func init MyFirstHttpFunction --dotnet

3. Move into the project directory:

cd MyHttpFunctionProject

Step 2: Add an HTTP Trigger Function

  • Run the following command to add a new HTTP Trigger function
func new
  • Choose “HTTP trigger” as the template.
  • Give your function a name, like “MyFirstHelloFunction.”
  • Decide whether your function should be “Anonymous” (public) or require a “Function” key for access.

Step 3: Write Your Function Logic

Open the MyFirstHelloFunction.cs file in your project directory. Here we can write the function logic.

using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using System.Text.Json;

public static class MyFirstHelloFunction
{
[FunctionName("MyFirstHelloFunction")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "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 = JsonSerializer.Deserialize<dynamic>(requestBody);
name = name ?? data?.name;

return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}!")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body.");
}
}

Step 4: Test Your Function Locally

Once the code is written, then it’s time to test / run the project locally in our machine.

  • Open a terminal window and navigate to your project directory.
  • Run the following command to start the local development server.
func start

This will build the project and the function will run locally. To test it, use Postman and send requests to http://localhost:7071/api/MyFirstHelloFunction.

Step 5: Deploy to Azure

  • In your terminal, navigate to the project directory.
  • Run the following command to deploy your function to Azure:
func azure functionapp publish <your-function-app-name>

Replace <your-function-app-name> with your Azure Function App’s name.

That’s It!

Congratulations! You’ve built your first Azure Function with an HTTP Trigger using C# .NET. You now have a serverless API ready to be accessed. Feel free to explore further, add more functionality, and integrate with other Azure services.

--

--

Dhanesh Kumar MJ

Engineer @ Phases.io| Traveler | Story Teller | Spiritual Being