Azure Function using C# in azure portal

Raviraj Jayaprakasam 40 Reputation points
2023-08-14T13:01:57.03+00:00

I'm a beginner, trying to implement a http triggered azure function using C# in portal where I need implement addition of two parameters. Along with this kindly guide me where to start exploring the function app documentation using C# functions and properties of triggers to call.

Thankyou.,

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,704 questions
0 comments No comments
{count} votes

Accepted answer
  1. JananiRamesh-MSFT 29,246 Reputation points
    2023-08-14T13:28:52.12+00:00

    Hi RAVIRAJ Thanks for reaching out. Please refer the below code for the addition of two parameters

    using System.Net;

    using Microsoft.AspNetCore.Mvc;

    using Microsoft.Extensions.Logging;

    public static async Task<IActionResult> Run(HttpRequest req, ILogger log)

    {

    log.LogInformation("C# HTTP trigger function processed a request.");
    
    string firstNumber = req.Query["firstNumber"];
    
    string secondNumber = req.Query["secondNumber"];
    
    int sum = int.Parse(firstNumber) + int.Parse(secondNumber);
    
    return new OkObjectResult(sum);
    

    }

    This code retrieves the values of the "firstNumber" and "secondNumber" parameters from the query string of the HTTP request, adds them together, and returns the result as an HTTP response.

    for exploring the Function App documentation using C# functions and properties of triggers, I recommend starting with the following resources:

    https://learn.microsoft.com/en-us/azure/azure-functions/functions-overview?pivots=programming-language-csharp

    https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp?tabs=functionsv2

    https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=python-v2%2Cin-process%2Cfunctionsv2&pivots=programming-language-csharp

    Let me know incase of further quereis, I would be happy to assist you.

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Thiago Custodio 196 Reputation points
    2023-08-14T13:30:11.7666667+00:00

    AFAIK, creating functions through portal is a deprecated feature. You can go with Visual Studio (community) / Visual Studio Code (free).

    In the official doc, there are multiple samples of Azure Functions with more than one parameter. Here's a quick excerpt from one of them:

            [FunctionName("TurbineRepair")]
            [OpenApiOperation(operationId: "Run")]
            [OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
            [OpenApiRequestBody("application/json", typeof(RequestBodyModel), 
                Description = "JSON request body containing { hours, capacity}")]
            [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(string),
                Description = "The OK response message containing a JSON result.")]
            public static async Task<IActionResult> Run(
                [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
                ILogger log)
            {
                // Get request body data.
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                dynamic data = JsonConvert.DeserializeObject(requestBody);
                int? capacity = data?.capacity;
                int? hours = data?.hours;
    

    In the previous code, capacity and hours are being passed as parameters through the http request body. You can pass multiple parameters through querystring too. Visit the official doc in the samples section for more code samples.

    https://learn.microsoft.com/en-us/azure/azure-functions/openapi-apim-integrate-visual-studio

    1 person found this answer helpful.
    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.