Skip to main content

Deploy ASP.NET Core

This guide will help you deploy an ASP.NET Core application to Deployxa.

Overview

ASP.NET Core is a cross-platform, high-performance framework for building modern, cloud-based applications. Deployxa provides optimized deployments for ASP.NET Core with automatic configuration.

Features:

  • Automatic framework detection
  • .NET 8+ support
  • PostgreSQL, MySQL, SQL Server support
  • Entity Framework Core
  • Automatic HTTPS
  • Global CDN

Prerequisites

  • An ASP.NET Core application (.NET 8 recommended)
  • A GitHub repository with your code
  • A Deployxa account

Quick Start

1. Create Your ASP.NET Core App

If you don't have an ASP.NET Core app yet:

dotnet new webapi -n my-aspnet-app
cd my-aspnet-app

Or for MVC:

dotnet new mvc -n my-aspnet-app
cd my-aspnet-app

2. Configure for Production

Update appsettings.Production.json:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Host=${DB_HOST};Database=${DB_NAME};Username=${DB_USER};Password=${DB_PASSWORD}"
}
}

3. Push to GitHub

git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/yourusername/my-aspnet-app.git
git push -u origin main

4. Deploy to Deployxa

  1. Go to Deployxa Dashboard
  2. Click "New Project"
  3. Select your GitHub repository
  4. Deployxa will automatically detect ASP.NET Core
  5. Click "Deploy"

Your app will be live in under 60 seconds!

Configuration

Detected Settings

Deployxa automatically detects these settings:

SettingValue
FrameworkASP.NET Core
Build Commanddotnet publish -c Release -o out
Output Directoryout
Start Commanddotnet out/*.dll
Port8080

Custom Configuration

If you need to customize settings:

  1. Go to ProjectSettingsBuild
  2. Modify settings as needed
  3. Save changes

Example Custom Settings:

{
"buildCommand": "dotnet publish -c Release -o out",
"outputDirectory": "out",
"startCommand": "dotnet out/MyApp.dll"
}

Environment Variables

Adding Environment Variables

  1. Go to ProjectEnvironment
  2. Click "Add Variable"
  3. Enter key and value
  4. Click "Save"

Common ASP.NET Core Environment Variables:

ASPNETCORE_ENVIRONMENT=Production
ASPNETCORE_URLS=http://+:8080

# Database
DB_HOST=your-db-host
DB_NAME=your-database
DB_USER=your-username
DB_PASSWORD=your-password

# Connection String
ConnectionStrings__DefaultConnection=Host=${DB_HOST};Database=${DB_NAME};Username=${DB_USER};Password=${DB_PASSWORD}

Using Environment Variables

In your ASP.NET Core app:

// Program.cs
var builder = WebApplication.CreateBuilder(args);

// Access environment variable
var dbHost = Environment.GetEnvironmentVariable("DB_HOST");

// Or use configuration
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");

var app = builder.Build();
app.Run();
// appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Host=${DB_HOST};Database=${DB_NAME};Username=${DB_USER};Password=${DB_PASSWORD}"
}
}

Database Configuration

Using PostgreSQL with Entity Framework Core

Add packages:

dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
dotnet add package Microsoft.EntityFrameworkCore.Design

Configure in Program.cs:

builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));

Create models:

public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}

Create context:

public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

public DbSet<User> Users { get; set; }
}

Database Migrations

Create initial migration:

dotnet ef migrations add InitialCreate

Apply migrations:

dotnet ef database update

Or in code:

using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
db.Database.Migrate();
}

Using MySQL

Add package:

dotnet add package Pomelo.EntityFrameworkCore.MySql

Configure:

builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(
builder.Configuration.GetConnectionString("DefaultConnection"),
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("DefaultConnection"))
));

Custom Domains

Adding a Custom Domain

  1. Go to ProjectDomains
  2. Click "Add Domain"
  3. Enter your domain (e.g., example.com)
  4. Configure DNS records
  5. Wait for verification

DNS Configuration

For Apex Domain (example.com):

Type: A
Name: @
Value: 76.76.21.21

For Subdomain (www.example.com):

Type: CNAME
Name: www
Value: cname.deployxa.com

Performance Optimization

Enable Response Compression

Add package:

dotnet add package Microsoft.AspNetCore.ResponseCompression

Configure in Program.cs:

builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
});

var app = builder.Build();
app.UseResponseCompression();

Enable Caching

Configure in Program.cs:

builder.Services.AddResponseCaching();

var app = builder.Build();
app.UseResponseCaching();

Use caching in controllers:

[ResponseCache(Duration = 60)]
[HttpGet]
public IActionResult GetData()
{
return Ok(data);
}

Database Connection Pooling

Entity Framework Core uses connection pooling by default. Configure in Program.cs:

builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(connectionString, sqlOptions =>
{
sqlOptions.MaxBatchSize(128);
}));

Monitoring

Viewing Logs

  1. Go to ProjectLogs
  2. Select log type:
    • Build Logs: Deployment process
    • Runtime Logs: Application output
    • Error Logs: Application errors

ASP.NET Core Logging

Configure logging in appsettings.json:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

Use logging in code:

public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}

public IActionResult Index()
{
_logger.LogInformation("Home page accessed");
return View();
}
}

Health Checks

Add package:

dotnet add package Microsoft.AspNetCore.Diagnostics.HealthChecks

Configure in Program.cs:

builder.Services.AddHealthChecks();

var app = builder.Build();
app.MapHealthChecks("/health");

Monitoring Metrics

  1. Go to ProjectAnalytics
  2. View metrics:
    • Request count
    • Response times
    • Error rates
    • Bandwidth usage
    • CPU/RAM usage

Troubleshooting

500 Internal Server Error

Problem: Application returns 500 error

Solution:

  1. Check runtime logs for errors
  2. Verify environment variables
  3. Check database connection
  4. Ensure application starts correctly
  5. Check for missing dependencies

Database Connection Failed

Problem: Npgsql.NpgsqlException or MySqlException

Solution:

  1. Verify connection string is correct
  2. Check database host and port
  3. Ensure database exists
  4. Check firewall rules
  5. Verify database credentials

Build Failed

Problem: dotnet publish fails

Solution:

  1. Check build logs for specific errors
  2. Verify dependencies are correct
  3. Check for syntax errors
  4. Test build locally
  5. Ensure .NET SDK version is compatible

Application Crashes on Start

Problem: Application crashes immediately

Solution:

  1. Check runtime logs for errors
  2. Verify environment variables
  3. Check for missing configuration
  4. Ensure ASPNETCORE_URLS is configured correctly
  5. Check for port conflicts

Slow Response Times

Problem: Application responds slowly

Solution:

  1. Enable response compression
  2. Use caching
  3. Optimize database queries
  4. Use async/await properly
  5. Upgrade plan for more resources

Best Practices

Project Structure

my-aspnet-app/
├── Controllers/
│ └── HomeController.cs
├── Models/
│ └── User.cs
├── Data/
│ └── ApplicationDbContext.cs
├── Migrations/
├── Views/
├── appsettings.json
├── appsettings.Production.json
├── Program.cs
├── my-aspnet-app.csproj
└── README.md

Environment Variables

  • Never commit secrets
  • Use .env.example as template
  • Rotate secrets regularly
  • Use different values per environment

Security

  • Keep .NET updated
  • Use HTTPS only
  • Validate all inputs
  • Use parameterized queries
  • Enable CORS properly
  • Use authentication/authorization
  • Keep dependencies updated

Performance

  • Enable response compression
  • Use caching
  • Optimize database queries
  • Use async/await
  • Use connection pooling
  • Enable HTTP/2

Database

  • Use migrations for schema changes
  • Index frequently queried columns
  • Use transactions for complex operations
  • Backup database regularly
  • Use connection pooling

Advanced Configuration

Custom Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddHealthChecks();
builder.Services.AddResponseCompression();

var app = builder.Build();

// Configure middleware
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseResponseCompression();
app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.MapHealthChecks("/health");

// Apply migrations
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
db.Database.Migrate();
}

app.Run();

Docker Support

For local development with Docker:

FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY out .
ENTRYPOINT ["dotnet", "MyApp.dll"]

API Versioning

Add package:

dotnet add package Asp.Versioning.Mvc

Configure:

builder.Services.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(1, 0);
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true;
});

Examples

Basic ASP.NET Core App

// Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello, Deployxa!");

app.Run();

REST API

// Controllers/UsersController.cs
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
private readonly ApplicationDbContext _context;

public UsersController(ApplicationDbContext context)
{
_context = context;
}

[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> GetUsers()
{
return await _context.Users.ToListAsync();
}

[HttpPost]
public async Task<ActionResult<User>> CreateUser(User user)
{
_context.Users.Add(user);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetUsers), new { id = user.Id }, user);
}
}

With Database

// Data/ApplicationDbContext.cs
using Microsoft.EntityFrameworkCore;

public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

public DbSet<User> Users { get; set; }
}
// Models/User.cs
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}

Resources


Need Help? Contact support@deployxa.com or join our community Discord.