Day 12 of 30-Day .NET Challenge: Azure Functions
Tags: beginners, dotnet, programming, csharp
Published: 2024-03-31 18:30:55

## Introduction

Azure Functions are serverless applications on Microsoft Azure Cloud Platform without worrying about the infrastructure to run it. It’s very similar to the lambda function in the AWS Cloud.

### Learning Objectives

* Create a function using Visual Studio or Visual Studio Code

* Run an Azure function locally.

### Prerequisites for Developers

* Experience with Visual Studio

* Basic understanding of C# Programming Language

## Getting Started

### Add a new function

 1. Choose a project “Azure Function” from Visual Studio.

 2. Select .Net 6 as the target version

 3. Select the template “HTTP Trigger”

 4. Provide a function name.

 5. Choose Authorization Level as “Anonymous” which allows access to anyone to call your function endpoint.

### Code

The below Azure Function returns a string message as follows

* If ?name= is passed then returns a message as Hello, {name}. This HTTP triggered function executed successfully.

* Else a general message is returned This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.
```csharp
    public static class BasicExample
    {
        [FunctionName("BasicExample")]
        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);
        }
    }
```
### Test the function locally

Simply press F5 to start debugging the Azure function, it will open a console which will provide a URL to access the browser.

### Console Output
```
    Azure Functions Core Tools
    Core Tools Version:       4.0.5198 Commit hash: N/A  (64-bit)
    Function Runtime Version: 4.21.1.20667
    
    [2024-03-28T05:48:45.707Z] Found D:\Workspace\30DayChallenge.Net\AzureFunctionExample\AzureFunctionExample.csproj. Using for user secrets file configuration.
```

    Functions:

            BasicExample: [GET,POST] http://localhost:7073/api/BasicExample

### Open the URL

Open the URL “http://localhost:7073/api/BasicExample” in the browser to start running the function endpoint.

The response returned from the browser.

    This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.

### Add query parameters

Modify the URL with additional query parameters as ?name=Sukhpinder
```csharp
    http://localhost:7073/api/BasicExample?name=Sukhpinder
```
The response returned from the browser.
```
    Hello, Sukhpinder. This HTTP triggered function executed successfully.
```
## Complete Code on GitHub
[GitHub — ssukhpinder/30DayChallenge.Net](https://github.com/ssukhpinder/30DayChallenge.Net/tree/main)

### C# Programming🚀

Thank you for being a part of the C# community! Before you leave:

#### If you’ve made it this far, please show your appreciation with a clap and follow the author! 👏️️

Follow us: [X](https://twitter.com/sukhsukhpinder) | [LinkedIn](https://www.linkedin.com/in/sukhpinder-singh/) | [Dev.to](https://dev.to/ssukhpinder) | [Hashnode](https://dotnet.hashnode.dev/) | [Newsletter](https://www.linkedin.com/newsletters/net-programming-7031098498754195456/) | [Tumblr](https://www.tumblr.com/settings/blog/codewithsukh)

Visit our other platforms: [GitHub](https://github.com/ssukhpinder) | [Instagram](https://www.instagram.com/codewithsukh/) | [Tiktok](https://www.tiktok.com/@codewithsukh) | [Quora](https://www.quora.com/profile/Sukhpinder-Singh-4) | [Daily.dev](https://app.daily.dev/devcard)

More content at [C# Programming](https://medium.com/c-sharp-progarmming)