Generative AI has transformed how developers build intelligent applications. From content generation and code suggestions to chatbots and personalized recommendations, generative AI enables web applications to provide dynamic and context-aware responses.
In this article, we will explore how to integrate generative AI in ASP.NET Core web applications, focusing on architecture, AI integration, security, and best practices.
1. Understanding Generative AI
Generative AI refers to models capable of producing new content such as text, images, or audio based on input data or prompts. Examples include:
Text generation (chat responses, article summaries)
Code generation or completion
Image creation from textual prompts
Data synthesis for testing or training
Benefits for web applications:
Automates content creation
Enhances user engagement with personalized experiences
Supports intelligent assistants and chatbots
2. Architecture Overview
A typical ASP.NET Core web app with generative AI looks like this:
Angular / Blazor / React Frontend
|
| HTTP / WebSocket
v
ASP.NET Core Web API
|
| AI Service (External API / Internal Model)
v
Generative AI Model (OpenAI, Azure OpenAI, ML.NET, HuggingFace)
Frontend – collects user input, displays generated content
Backend – manages requests, integrates with AI, applies business rules
AI Service – generates output based on prompts, can be hosted externally or internally
3. Selecting a Generative AI Model
External API Providers
OpenAI GPT Models – text generation, summarization, chat
Azure OpenAI Service – enterprise-ready GPT integration
HuggingFace Inference API – open-source transformer models
Internal Models
Recommendation: Use external APIs for production-ready capabilities unless data privacy requires on-premise deployment.
4. ASP.NET Core Backend Integration
4.1 Installing Dependencies
Use HttpClientFactory for robust API calls:
builder.Services.AddHttpClient<IAiService, OpenAiService>();
4.2 AI Service Interface
public interface IAiService
{
Task<string> GenerateTextAsync(string prompt);
}
4.3 AI Service Implementation (OpenAI Example)
public class OpenAiService : IAiService
{
private readonly HttpClient _httpClient;
private readonly IConfiguration _config;
public OpenAiService(HttpClient httpClient, IConfiguration config)
{
_httpClient = httpClient;
_config = config;
}
public async Task<string> GenerateTextAsync(string prompt)
{
var request = new
{
model = "gpt-4",
prompt = prompt,
max_tokens = 200
};
var response = await _httpClient.PostAsJsonAsync("https://api.openai.com/v1/completions", request);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadFromJsonAsync<JsonElement>();
return content.GetProperty("choices")[0].GetProperty("text").GetString();
}
}
4.4 API Endpoint for Frontend
[ApiController]
[Route("api/[controller]")]
public class AiController : ControllerBase
{
private readonly IAiService _aiService;
public AiController(IAiService aiService)
{
_aiService = aiService;
}
[HttpPost("generate")]
public async Task<IActionResult> Generate([FromBody] AiRequest request)
{
if (string.IsNullOrWhiteSpace(request.Prompt))
return BadRequest("Prompt cannot be empty.");
var result = await _aiService.GenerateTextAsync(request.Prompt);
return Ok(new { text = result });
}
}
AiRequest DTO:
public class AiRequest
{
public string Prompt { get; set; }
}
5. Angular Frontend Implementation
5.1 Form to Collect User Input
<form [formGroup]="aiForm" (ngSubmit)="submitPrompt()">
<textarea formControlName="prompt" placeholder="Enter prompt"></textarea>
<button type="submit">Generate</button>
</form>
<div *ngIf="generatedText">
<h3>Generated Output:</h3>
<p>{{generatedText}}</p>
</div>
5.2 Calling API
submitPrompt() {
const prompt = this.aiForm.value.prompt;
this.http.post<{text: string}>('/api/ai/generate', { prompt })
.subscribe(res => this.generatedText = res.text);
}
6. Handling Long-Running AI Tasks
Use background processing with queues for complex or large prompts
ASP.NET Core supports IHostedService or Hangfire for background jobs
Notify frontend via SignalR when generation completes
7. Security Considerations
Validate user input – prevent malicious or inappropriate prompts
API Key Protection – never expose AI API keys in frontend
Rate Limiting – avoid abuse or excessive costs
Data Privacy – avoid sending sensitive data to external APIs unless encrypted
8. Performance and Cost Optimization
Cache repeated prompts to reduce API calls
Limit max tokens and request frequency
Use smaller models for minor tasks (e.g., summarization)
Monitor API usage to avoid unexpected charges
9. Real-world Use Cases
Chatbots – AI responses in customer support portals
Content Generation – auto-generating articles, blogs, or product descriptions
Code Assistance – generating boilerplate code or SQL queries
Summarization – summarizing long text documents
Interactive UI Suggestions – autocomplete, recommendation, or dynamic prompts
10. Best Practices
Always log requests (without storing sensitive user data) for debugging
Provide fallback content in case AI service fails
Implement user feedback mechanisms to improve AI usefulness
Monitor AI performance and update models or prompts regularly
Secure endpoints with JWT/OAuth2
11. Summary
Integrating generative AI into ASP.NET Core web applications provides powerful, intelligent features. Key steps include:
Choose an AI model (external API or internal)
Implement a backend service to manage AI requests
Securely integrate AI in the frontend
Handle long-running tasks and background processing
Apply security, caching, and monitoring best practices
By following these guidelines, developers can deliver AI-driven applications that are dynamic, secure, and user-friendly.