1

I have a stored procedure on Azure SQL database that I can execute with

EXEC [dbo].[PGET_JSON] @code = N'XYZ'

It returns FOR JSON result

{
  "XX": [
    {
      "DocOstatni": [
        {
          "Type": "Something",
          "Published": "2023-08-28T20:14:18.617",
          "Order": 1,
          "Name": "Something",
          "File": "PV_XXX_6.PDF",
        },{ ...

I need to create within Azure a (.net?) application that would expose the result of this stored procedure on public URL with parameter @code like

myappid.azurewebsites.net/XYZ

that would return the output of stored procedure [PGET_JSON] with parameter @code='XYZ'.

What do I need on Azure and what type of Azure service should be used for it, should I use Azure web application or something else, what would be the easiest and least expensive solution?

I already have an Azure web app and perhaps It would be better to add the one sipmle API to the existing app?

2
  • 2
    Consider an Azure Function to build a serverless REST API. Commented Nov 15, 2023 at 14:05
  • 1
    If I already have an Azure web app, would not it be much less expensive to add the rest api to the existing app? Commented Nov 15, 2023 at 15:09

1 Answer 1

1

If I already have an Azure web app, would not it be much less expensive to add the rest api to the existing app?

Having a consumption-based function app is less expensive than using Azure Web app.

As @Dan Guzmann suggested to use Azure Function.

I have used Azure function to get the stored procedure. It worked for me.

#My Stored Procedure enter image description here

I have used HTTP Trigger in azure function.

#My code:

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;
using System.Data;
using Microsoft.Data.SqlClient;

namespace FunctionApp6
{
    public static class Function1
    {
        [FunctionName("Function1")]
        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 code = req.Query["code"];
            if (string.IsNullOrEmpty(code))
            {
                return new BadRequestObjectResult("Please provide a valid code parameter.");
            }

            string connectionString = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                using (SqlCommand command = new SqlCommand("PGET_JSON", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@code", code);

                    using (SqlDataAdapter adapter = new SqlDataAdapter (command))
                    {
                        DataTable dataTable = new DataTable();
                        adapter.Fill(dataTable);

                        if (dataTable.Rows.Count>0)
                        {
                            
                            string result = JsonConvert.SerializeObject(dataTable);

                            return new OkObjectResult(result);
                        }
                        else
                        {
                            return new NotFoundObjectResult("No data found");
                        }
                    }
                }
            }
        }
    }
}

OUTPUT:

[{"Id":123,"Name":"Vivek"}]

#Local: enter image description here

enter image description here

#AZURE:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I am using existing web app right now... But the routing stops working after publishing to Azure...
@VojtěchDohnal Could you please let me know which framework web app you using or is it .net console app

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.