Azure Blob Storage & Logic App Integration

Step-by-Step: Controller to Azure Blob to Logic App

Step 1: Setup Azure Blob Storage

  1. Go to Azure Portal & search for Storage Accounts.
  2. Create a new storage account (or use existing).
  3. Under "Containers", create a container (e.g., datafiles).
  4. Upload a sample file (e.g., JSON or CSV).

Step 2: Create Logic App Trigger

  1. Create a new Logic App (Consumption) from Azure Portal.
  2. Choose the trigger: When a blob is added or modified (properties only).
  3. Select your storage account and the container (e.g., datafiles).
  4. Add a step: Get blob content.
  5. Filter content with a Condition step.
  6. On "true", insert into SQL DB using "SQL Server – Insert row" connector.

Step 3: Add Connection String in appsettings.json


"AzureBlobStorage": {
  "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=youraccount;AccountKey=yourkey;EndpointSuffix=core.windows.net",
  "ContainerName": "datafiles"
}
    

Step 4: Create Blob Service in .NET


    using System.Text.Json;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using blob_access.Models;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace blob_access.Controllers
{
    public class StudentController : Controller
    {
        private readonly string connectionString;
        private readonly string containerName;
        private readonly string blobFileName;

        public StudentController(IConfiguration configuration)
        {
            connectionString = configuration["AzureStorage:ConnectionString"];
            containerName = configuration["AzureStorage:ContainerName"];
            blobFileName = configuration["AzureStorage:BlobFileName"];
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public async Task Save(Student student)
        {
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

            await containerClient.CreateIfNotExistsAsync(PublicAccessType.None);

            BlobClient blobClient = containerClient.GetBlobClient(blobFileName);

            List students;

            if (await blobClient.ExistsAsync())
            {
                var download = await blobClient.DownloadAsync();
                using (var reader = new StreamReader(download.Value.Content))
                {
                    string content = await reader.ReadToEndAsync();
                    students = JsonSerializer.Deserialize>(content) ?? new List();
                }
            }
            else
            {
                students = new List();
            }

            students.Add(student);

            using (MemoryStream ms = new MemoryStream())
            {
                await JsonSerializer.SerializeAsync(ms, students);
                ms.Position = 0;
                await blobClient.UploadAsync(ms, overwrite: true);
            }

            ViewBag.Message = "Student data saved successfully!";
            return View("Index");
        }

        [HttpGet]
        public async Task ViewStudents()
        {
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
            BlobClient blobClient = containerClient.GetBlobClient(blobFileName);

            List students = new List();

            if (await blobClient.ExistsAsync())
            {
                var download = await blobClient.DownloadAsync();
                using (var reader = new StreamReader(download.Value.Content))
                {
                    string content = await reader.ReadToEndAsync();
                    students = JsonSerializer.Deserialize>(content) ?? new List();
                }
            }

            return View(students);
        }
        [HttpGet]
        public async Task Edit(int id)
        {
            var student = await GetStudentById(id);
            if (student == null)
            {
                return NotFound();
            }
            return View(student);
        }

        [HttpPost]
        public async Task Edit(Student updatedStudent)
        {
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
            BlobClient blobClient = containerClient.GetBlobClient(blobFileName);

            List students = new List();

            if (await blobClient.ExistsAsync())
            {
                var download = await blobClient.DownloadAsync();
                using (var reader = new StreamReader(download.Value.Content))
                {
                    string content = await reader.ReadToEndAsync();
                    students = JsonSerializer.Deserialize>(content) ?? new List();
                }
            }

            var studentIndex = students.FindIndex(s => s.RollNumber == updatedStudent.RollNumber);
            if (studentIndex != -1)
            {
                students[studentIndex] = updatedStudent;
            }

            using (MemoryStream ms = new MemoryStream())
            {
                await JsonSerializer.SerializeAsync(ms, students);
                ms.Position = 0;
                await blobClient.UploadAsync(ms, overwrite: true);
            }

            return RedirectToAction("ViewStudents");
        }

        private async Task GetStudentById(int id)
        {
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
            BlobClient blobClient = containerClient.GetBlobClient(blobFileName);

            if (await blobClient.ExistsAsync())
            {
                var download = await blobClient.DownloadAsync();
                using (var reader = new StreamReader(download.Value.Content))
                {
                    string content = await reader.ReadToEndAsync();
                    var students = JsonSerializer.Deserialize>(content) ?? new List();
                    return students.FirstOrDefault(s => s.RollNumber == id);
                }
            }
            return null;
        }
    }
}

    

Step 5: CSHTML View (Index.cshtml)


@model blob_access.Models.Student

            @{
                ViewBag.Title = "Student Entry";
            }

<h2>Enter Student Details</h2>

<form asp-controller="Student" asp-action="Save" method="post">
    <div>
        <label>Name:</label>
        <input type="text" name="Name" required />
    </div>
    <div>
        <label>Roll Number:</label>
        <input type="number" name="RollNumber" required />
    </div>
    <div>
        <label>Total Marks:</label>
        <input type="number" name="TotalMarks" required />
    </div>
    <button type="submit">Save</button>
</form>

            @if (ViewBag.Message != null)
            {
                < p style = "color:green" & gt;
                @ViewBag.Message
                </ p & gt;
            }

<a href="/Student/ViewStudents">View Stored Students</a>

Step 6: CSHTML View (Edit.html)


@model blob_access.Models.Student

            @{
                ViewBag.Title = "Edit Student";
            }

<h2>Edit Student</h2>

<form asp-controller="Student" asp-action="Edit" method="post">
    <div>
        <label>Name:</label>
        <input type="text" name="Name" value="@Model.Name" required />
    </div>
    <div>
        <label>Roll Number:</label>
        <input type="number" name="RollNumber" value="@Model.RollNumber" readonly />
    </div>
    <div>
        <label>Total Marks:</label>
        <input type="number" name="TotalMarks" value="@Model.TotalMarks" required />
    </div>
    <button type="submit">Update</button>
</form>

<a href="/Student/ViewStudents">Back to List</a>

Step 7: CSHTML View (ViewStudents.cshtml)


@model List<blob_access.Models.Student>

@{
    ViewBag.Title = "Student List";
}

<h2>Stored Student Records</h2>

@if (Model.Count > 0)
{
    <table border="1" cellpadding="5">
        <thead>
            <tr>
                <th>Name</th>
                <th>Roll Number</th>
                <th>Total Marks</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var student in Model)
            {
                <tr>
                    <td>@student.Name</td>
                    <td>@student.RollNumber</td>
                    <td>@student.TotalMarks</td>
                    <td>
                        <a href="@Url.Action("Edit", "Student", new { id = student.RollNumber })">Edit</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>
}
else
{
    <p>No student data found.</p>
}

<a href="/Student/Index">Back to Entry Form</a>

Step 8: Model-> Students.cs


        namespace blob_access.Models
{
    public class Student
    {
        public string Name { get; set; }
        public int RollNumber { get; set; }
        public int TotalMarks { get; set; }
    }

}

    

Step 9: Add Dependency in Startup.cs or Program.cs


// For .NET 6+
builder.Services.AddSingleton<AzureBlobService>();