Artificial Intelligence is rapidly transforming modern software development. From AI chatbots and intelligent search systems to AI-powered document processing and recommendation engines, developers are now integrating AI into almost every type of application.
With ASP.NET Core and OpenAI APIs, C# developers can build scalable, secure, cloud-native AI applications using familiar technologies.
Modern AI applications are no longer limited to large AI research companies. Today, any developer can build:
AI chatbots
AI copilots
AI-powered APIs
Document summarization systems
AI search engines
AI content generation tools
AI customer support systems
Enterprise AI assistants
AI automation workflows
In this article, we will explore how to build AI applications in ASP.NET Core using OpenAI APIs with practical examples, architecture guidance, security recommendations, and production-ready best practices.
Why ASP.NET Core Is Ideal for AI Applications
ASP.NET Core provides an excellent foundation for AI-powered systems because it offers:
High performance
Cross-platform support
Cloud-native architecture
Scalable APIs
Dependency injection
Minimal APIs
Built-in security
Container support
Microservices compatibility
Combining ASP.NET Core with OpenAI APIs enables developers to build enterprise-grade AI applications efficiently.
Common AI Application Use Cases
Here are some practical AI application scenarios developers commonly build.
| AI Use Case | Example |
|---|---|
| AI Chatbot | Customer support assistant |
| Text Summarization | Summarizing documents |
| AI Search | Semantic search engine |
| Content Generation | Blog or email generation |
| AI Coding Assistant | Developer productivity tools |
| AI Recommendations | Product recommendation systems |
| AI Automation | Workflow automation |
| AI Knowledge Base | Enterprise assistant |
Understanding OpenAI APIs
OpenAI APIs provide access to advanced AI models that can:
Generate text
Analyze content
Answer questions
Summarize data
Generate code
Perform reasoning tasks
Process structured prompts
Popular OpenAI capabilities include:
Chat Completions
Embeddings
Function Calling
Streaming Responses
Structured Outputs
Vision APIs
AI Agents
These APIs can easily integrate into ASP.NET Core applications.
Creating an ASP.NET Core AI Project
Start by creating a new ASP.NET Core Web API project.
dotnet new webapi -n AIApplication
Move into the project directory.
cd AIApplication
Run the project.
dotnet run
Installing OpenAI SDK
Install the OpenAI NuGet package.
dotnet add package OpenAI
You can also use:
Azure OpenAI SDK
Semantic Kernel
Microsoft.Extensions.AI
Depending on your architecture requirements.
Configuring API Keys Securely
Never hardcode API keys directly in source code.
Use appsettings.json.
{
"OpenAI": {
"ApiKey": "YOUR_API_KEY"
}
}
Better production approaches include:
Azure Key Vault
AWS Secrets Manager
Environment Variables
Kubernetes Secrets
Registering OpenAI Services
In Program.cs:
using OpenAI.Chat;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(serviceProvider =>
{
var configuration = serviceProvider
.GetRequiredService<IConfiguration>();
var apiKey = configuration["OpenAI:ApiKey"];
return new ChatClient(
model: "gpt-4.1",
apiKey: apiKey);
});
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();
This registers the OpenAI client using dependency injection.
Building Your First AI API
Now create a controller.
using Microsoft.AspNetCore.Mvc;
using OpenAI.Chat;
namespace AIApplication.Controllers;
[ApiController]
[Route("api/[controller]")]
public class ChatController : ControllerBase
{
private readonly ChatClient _chatClient;
public ChatController(ChatClient chatClient)
{
_chatClient = chatClient;
}
[HttpPost]
public async Task<IActionResult> AskQuestion(string question)
{
var response = await _chatClient.CompleteChatAsync(question);
return Ok(response.Content[0].Text);
}
}
This creates a simple AI-powered API endpoint.
Testing the API
Example request:
POST /api/chat?question=Explain dependency injection in ASP.NET Core
Example response:
Dependency Injection is a design pattern used to achieve Inversion of Control...
You now have a working AI-powered ASP.NET Core application.
Creating AI Chatbots
One of the most common AI applications is a chatbot.
Chatbot Workflow
| Step | Description |
|---|---|
| User Sends Message | User enters a query |
| ASP.NET Core API Receives Request | Backend processes prompt |
| OpenAI API Generates Response | AI creates answer |
| API Returns Response | Frontend displays output |
Building a Context-Aware Chatbot
Basic AI calls are stateless.
To create conversational memory, maintain chat history.
var messages = new List<ChatMessage>
{
ChatMessage.CreateSystemMessage(
"You are an ASP.NET Core assistant."),
ChatMessage.CreateUserMessage(
"Explain middleware."),
ChatMessage.CreateAssistantMessage(
"Middleware handles HTTP requests."),
ChatMessage.CreateUserMessage(
"Give me an example.")
};
var response = await _chatClient.CompleteChatAsync(messages);
This enables contextual conversations.
Using System Prompts Effectively
System prompts define AI behavior.
Example:
ChatMessage.CreateSystemMessage(
"You are a senior C# architect helping developers.")
Good prompts improve:
Accuracy
Tone
Structure
Reliability
Response quality
Implementing Streaming Responses
Streaming creates real-time AI experiences similar to ChatGPT.
Benefits include:
Faster perceived responses
Better UX
Real-time interaction
Improved responsiveness
Example:
await foreach (var update in
_chatClient.CompleteChatStreamingAsync(question))
{
Console.Write(update.ContentUpdate[0].Text);
}
Streaming is especially useful for:
Chat applications
AI copilots
Live assistants
Long-form generation
AI-Powered Document Summarization
AI can summarize large documents efficiently.
Example:
Join the conversation! Your thoughts help the community grow.