Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Integrating Azure OpenAI Service with ASP.NET Core: A Comprehensive Guide

Integrating Azure OpenAI Service with ASP.NET Core: A Comprehensive Guide

Date- May 04,2026 62
azure openai

Overview

The Azure OpenAI Service provides developers with access to powerful AI models developed by OpenAI, such as GPT-3 and Codex. These models can generate human-like text based on prompts, making them invaluable for a wide range of applications, from customer support chatbots to content generation tools. By integrating these capabilities into applications, developers can automate and enhance user interactions, leading to improved efficiency and user satisfaction.

In real-world scenarios, businesses can leverage Azure OpenAI Service for use cases like generating personalized email responses, summarizing large documents, or even creating interactive conversational agents. The ability to easily integrate these advanced AI functionalities into existing platforms makes Azure OpenAI a compelling choice for modern software development.

Prerequisites

  • ASP.NET Core SDK: Ensure you have the latest version of the ASP.NET Core SDK installed on your machine.
  • Azure Subscription: A valid Azure account is necessary to access the OpenAI Service and create resources.
  • Basic C# Knowledge: Familiarity with C# programming is essential for implementing the integration.
  • NuGet Package Manager: Understanding how to manage NuGet packages in your ASP.NET Core project.
  • HTTP Client: Basic knowledge of making HTTP requests in .NET.

Setting Up Azure OpenAI Service

Before integrating Azure OpenAI Service into an ASP.NET Core application, you must set up the service in the Azure portal. This involves creating an Azure OpenAI resource that provides you with an endpoint and API key for authentication. The steps include logging into your Azure account, navigating to the Azure OpenAI Service resource, and creating a new resource. After creation, you will find your endpoint URL and access key, which are crucial for making API calls.

Once you have your Azure OpenAI Service set up, you can proceed to integrate it into your ASP.NET Core application. This integration typically involves making HTTP requests to the OpenAI API, sending prompts and receiving generated text as responses. Proper handling of these requests, including authentication and error management, is essential for a robust implementation.

public class OpenAIService
{
    private readonly HttpClient _httpClient;
    private readonly string _apiKey;
    private readonly string _endpoint;

    public OpenAIService(string apiKey, string endpoint)
    {
        _httpClient = new HttpClient();
        _apiKey = apiKey;
        _endpoint = endpoint;
        _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + _apiKey);
    }

    public async Task GenerateTextAsync(string prompt)
    {
        var requestBody = new
        {
            prompt = prompt,
            max_tokens = 100
        };

        var response = await _httpClient.PostAsJsonAsync(_endpoint, requestBody);
        response.EnsureSuccessStatusCode();

        var result = await response.Content.ReadAsAsync();
        return result.choices[0].text;
    }
}

This code defines a class called OpenAIService, which encapsulates the logic for interacting with the Azure OpenAI API. The constructor takes an API key and an endpoint URL, initializing an HttpClient instance to handle HTTP requests. The Authorization header is set to use the provided API key for authentication.

The GenerateTextAsync method takes a prompt as input and constructs a JSON request body containing the prompt and the maximum number of tokens to generate. It then sends a POST request to the OpenAI API endpoint and ensures the response is successful. Finally, it parses the response to extract the generated text and return it.

Handling Errors and Exceptions

Error handling is critical when dealing with external APIs. The OpenAI API can return various errors, such as rate limits or invalid requests. Implementing robust error handling will ensure that your application can gracefully handle such scenarios.

public async Task GenerateTextAsync(string prompt)
{
    try
    {
        var requestBody = new
        {
            prompt = prompt,
            max_tokens = 100
        };

        var response = await _httpClient.PostAsJsonAsync(_endpoint, requestBody);
        response.EnsureSuccessStatusCode();

        var result = await response.Content.ReadAsAsync();
        return result.choices[0].text;
    }
    catch (HttpRequestException e)
    {
        // Log the exception (e.g., to a file or monitoring system)
        Console.WriteLine(e.Message);
        return "Error generating text.";
    }
}

In this modified version of the GenerateTextAsync method, a try-catch block is used to catch any HttpRequestException that may occur during the API call. If an exception occurs, it logs the error message and returns a friendly error message to the caller. This approach helps maintain application stability and improves user experience.

Integrating Azure OpenAI with ASP.NET Core MVC

Integrating the Azure OpenAI Service into an ASP.NET Core MVC application allows you to create interactive web applications that utilize AI capabilities. This section will cover how to set up a simple MVC application that uses the OpenAI Service to generate text based on user input.

First, create a new ASP.NET Core MVC project. Then, add the OpenAIService class from the previous section. Next, create a controller that handles user requests and a view to capture user input.

public class HomeController : Controller
{
    private readonly OpenAIService _openAIService;

    public HomeController(OpenAIService openAIService)
    {
        _openAIService = openAIService;
    }

    [HttpGet]
    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public async Task GenerateText(string prompt)
    {
        var generatedText = await _openAIService.GenerateTextAsync(prompt);
        return View("Index", model: generatedText);
    }
}

This HomeController class initializes the OpenAIService through dependency injection. The Index action method returns the view for the main page. The GenerateText action method handles the form submission, calls the GenerateTextAsync method of the OpenAIService, and passes the generated text back to the view.

Creating the View

Next, create the corresponding Razor view for the Index action method to capture user input and display generated text.

Generate Text

@if (Model != null) {

Generated Text:

@Model

}

This Razor view contains a simple form where users can input a prompt. Upon submission, it posts the data to the GenerateText action in the controller. If the Model is not null (indicating that text has been generated), it displays the generated output.

Edge Cases & Gotchas

When integrating with external APIs like Azure OpenAI, developers should be aware of several edge cases that could affect the application’s performance and user experience. For instance, handling timeouts is critical, as network delays can result in unresponsive applications.

public async Task GenerateTextAsync(string prompt)
{
    try
    {
        var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
        var response = await _httpClient.PostAsJsonAsync(_endpoint, requestBody, cts.Token);
        response.EnsureSuccessStatusCode();
        var result = await response.Content.ReadAsAsync();
        return result.choices[0].text;
    }
    catch (OperationCanceledException)
    {
        return "Request timed out. Please try again.";
    }
    catch (HttpRequestException e)
    {
        Console.WriteLine(e.Message);
        return "Error generating text.";
    }
}

In this example, a CancellationTokenSource is used to set a timeout for the API request. If the request exceeds the specified time limit, an OperationCanceledException is thrown, allowing the application to respond appropriately. This practice prevents the application from hanging indefinitely.

Performance & Best Practices

When integrating Azure OpenAI Service, performance considerations must be taken into account. Since API calls can introduce latency, optimizing the number of requests and managing response times is crucial for a smooth user experience.

One effective strategy is to cache responses for prompts that are frequently requested. This approach reduces the number of API calls, thereby improving response times and saving costs. Implementing a caching mechanism can be done using in-memory caching or distributed caching solutions like Redis.

public class OpenAIService
{
    private readonly IMemoryCache _cache;

    public OpenAIService(IMemoryCache cache)
    {
        _cache = cache;
    }

    public async Task GenerateTextAsync(string prompt)
    {
        if (_cache.TryGetValue(prompt, out string cachedResponse))
        {
            return cachedResponse;
        }
        // Make API call and cache the response
    }
}

This code snippet demonstrates how to use IMemoryCache to store generated text responses. Before making an API call, it checks if the response for the given prompt is already cached. If so, it returns the cached response immediately, improving performance.

Real-World Scenario: Building a Chatbot

For a realistic application of the Azure OpenAI Service, consider building a simple chatbot that interacts with users based on their inputs. This chatbot will utilize the services we have discussed and demonstrate a complete integration from start to finish.

The chatbot will have a simple interface where users can enter their messages and receive responses generated by the Azure OpenAI Service. Begin by creating a new ASP.NET Core MVC project and set up the OpenAIService as shown previously. Next, add a new controller and view specifically for the chatbot functionality.

public class ChatbotController : Controller
{
    private readonly OpenAIService _openAIService;

    public ChatbotController(OpenAIService openAIService)
    {
        _openAIService = openAIService;
    }

    [HttpGet]
    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public async Task SendMessage(string userMessage)
    {
        var aiResponse = await _openAIService.GenerateTextAsync(userMessage);
        return Json(new { response = aiResponse });
    }
}

This ChatbotController handles GET requests to render the chatbot interface and POST requests to send user messages to the OpenAI service. Upon receiving a message, it generates a response and returns it as a JSON object.

Creating the Chatbot View

The corresponding view will contain a simple form for users to interact with the chatbot.

Chat with AI

This view includes a simple chat interface where users can type messages. The form submission is handled with jQuery to send an AJAX POST request to the SendMessage action in the ChatbotController. Upon receiving a response, it appends the AI's reply to the chatbox, providing an interactive experience.

Conclusion

  • Azure OpenAI Service integration allows developers to harness AI capabilities in their applications.
  • Setting up the service requires creating an Azure resource and configuring API access.
  • Proper error handling and performance optimization are crucial for a seamless user experience.
  • Real-world applications, such as chatbots, can significantly enhance user engagement.
  • Further learning can include exploring Azure services, advanced AI model tuning, and security practices.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

SignalR Integration in ASP.NET Core: Building a Real-Time WebSocket Chat Application
May 17, 2026
Integrating LinkedIn OAuth in ASP.NET Core for Professional Login
May 01, 2026
Integrating Plivo SMS API with ASP.NET Core: A Comprehensive Guide
Apr 29, 2026
Integrating Fast2SMS with ASP.NET Core for Reliable SMS Delivery in India
Apr 28, 2026
Previous in ASP.NET Core
Integrating Google Gemini API with ASP.NET Core: A Comprehensive …
Next in ASP.NET Core
Integrating Anthropic Claude API in ASP.NET Core for AI Chat and …
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    Complete Guide to C++ Classes: Explained with Examples 4,212 views
  • 2
    Implementing an End-to-End CI/CD Pipeline for ASP.NET Core… 367 views
  • 3
    Create Database and CRUD operation 3,388 views
  • 4
    Mastering TypeScript Utility Types: Partial, Required, Rea… 675 views
  • 5
    Responsive Slick Slider 23,373 views
  • 6
    Integrating Azure Cognitive Search into ASP.NET Core Appli… 156 views
  • 7
    Integrating Anthropic Claude API in ASP.NET Core for AI Ch… 141 views
  • 8
    How to get fcm server key 4,849 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26191 views
  • Exception Handling Asp.Net Core 20938 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 20391 views
  • How to implement Paypal in Asp.Net Core 19753 views
  • Task Scheduler in Asp.Net core 17705 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor