Creating Azure Function with Visual Studio 2022(Part 2)


We can create the azure function using Visual Studio 2022, VS Code and with Azure Portal website. But as Web Developers we will choose Visual Studio 2022. Visual Studio 2022 is one of the most powerful development editors.

Step 1: Create the new project for Azure function like this.

Step 2: Click on the Create Button of the wizard window. Now it will create basic scaffolding code for azure function.

Code snippets

[Function("Function1")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
    _logger.LogInformation("C# HTTP trigger function processed a request.");

    var response = req.CreateResponse(HttpStatusCode.OK);
    response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

    response.WriteString("Welcome to Azure Functions!");

    return response;
}

The Run method is the entry point of the function. It takes an HttpRequestData object as a parameter, representing the incoming HTTP request. The HttpTrigger attribute specifies that this function can be triggered by HTTP requests, and it allows both “get” and “post” methods.

var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

Here, a new HttpResponseData object is created using the CreateResponse method of the incoming HttpRequestData. The response status code is set to OK (200), and a “Content-Type” header is added to specify that the response will be in plain text with UTF-8 encoding.

Step 3: Run the application then you will see command window like this

Step 4: Now trigger the given GET and POST method from the postman. It will call the azure function code and return “Welcome to Azure Functions!”


Summary :

In this post we saw that how to create basic azure function. it is very simple and straight forward to create azure function with visual studio 2022. It is exactly similar to normal web api project. In next post we will see how to create CRUD Operation in Azure function.

One thought on “Creating Azure Function with Visual Studio 2022(Part 2)

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.