Machine learning (ML) is no longer just a research topic—it is widely used in real-world applications like recommendation systems, fraud detection, sentiment analysis, predictive analytics, and more. ASP.NET Core, being a modern and high-performance web framework, provides excellent ways to integrate ML models into production-ready applications.
In this article, we will explore how to integrate ML models into ASP.NET Core applications , covering key approaches, best practices, and real-world considerations.
1. Understanding the Integration Approaches
There are multiple ways to integrate machine learning models into ASP.NET Core applications:
1. Direct Integration using ML.NET
ML.NET is Microsoft’s native machine learning framework for .NET. It allows you to train, evaluate, and deploy models directly within your ASP.NET Core application.
Use cases:
Predicting user behavior
Classification tasks
Regression tasks
Recommendation engines
2. Using Pre-Trained Models via ONNX
ONNX (Open Neural Network Exchange) allows running pre-trained models from other frameworks like TensorFlow or PyTorch in .NET applications using Microsoft.ML.OnnxRuntime .
Use cases:
Image classification
Object detection
Text embeddings or sentiment analysis
3. Using External AI/ML APIs
You can also use cloud-based ML APIs like Azure Cognitive Services, OpenAI, or AWS ML APIs . These services host models and provide REST APIs that your ASP.NET Core app can consume.
Use cases:
Text-to-speech
Sentiment analysis
Language translation
Chatbots
2. Project Setup
Step 1: Create a New ASP.NET Core Web API
dotnet new webapi -n MLIntegrationApp
cd MLIntegrationApp
Step 2: Install Required Packages
For ML.NET :
dotnet add package Microsoft.ML
dotnet add package Microsoft.ML.DataView
For ONNX runtime :
dotnet add package Microsoft.ML.OnnxRuntime
If you plan to consume external ML APIs , install System.Net.Http.Json :
dotnet add package System.Net.Http.Json
3. Example 1: Using ML.NET for Sentiment Analysis
ML.NET allows you to train models in .NET and consume them directly.
Step 1: Define Data Models
Models/SentimentData.cs :
using Microsoft.ML.Data;
public class SentimentData
{
[LoadColumn(0)]
public string Text { get; set; } = string.Empty;
[LoadColumn(1), ColumnName("Label")]
public bool Label { get; set; }
}
public class SentimentPrediction
{
[ColumnName("PredictedLabel")]
public bool Prediction { get; set; }
public float Probability { get; set; }
public float Score { get; set; }
}
Step 2: Train the Model
using Microsoft.ML;
using Microsoft.ML.Data;
public class SentimentModel
{
private readonly string _dataPath = "Data/sentiment.csv";
private readonly string _modelPath = "Models/sentiment_model.zip";
private MLContext _mlContext;
private ITransformer? _model;
public SentimentModel()
{
_mlContext = new MLContext();
TrainModel();
}
private void TrainModel()
{
var data = _mlContext.Data.LoadFromTextFile<SentimentData>(_dataPath, hasHeader: true, separatorChar: ',');
var trainTestSplit = _mlContext.Data.TrainTestSplit(data, testFraction: 0.2);
var pipeline = _mlContext.Transforms.Text.FeaturizeText("Features", nameof(SentimentData.Text))
.Append(_mlContext.BinaryClassification.Trainers.SdcaLogisticRegression());
_model = pipeline.Fit(trainTestSplit.TrainSet);
_mlContext.Model.Save(_model, trainTestSplit.TrainSet.Schema, _modelPath);
}
public SentimentPrediction Predict(string input)
{
if (_model == null)
{
_model = _mlContext.Model.Load(_modelPath, out _);
}
var engine = _mlContext.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(_model);
return engine.Predict(new SentimentData { Text = input });
}
}
Step 3: Create API Controller
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class SentimentController : ControllerBase
{
private readonly SentimentModel _model;
public SentimentController()
{
_model = new SentimentModel();
}
[HttpPost]
public IActionResult Predict([FromBody] string text)
{
var prediction = _model.Predict(text);
return Ok(prediction);
}
}

Join the conversation! Your thoughts help the community grow.