PROWAREtech

articles » current » asp-net-core » kestrel » add-mime-types

ASP.NET Core: Add Content-Types or MIME-Types to Kestrel Server

Add file MIME-Types for Kestrel server static files; written in C#.

This article is about adding MIME-Types or Content-Types to Kestrel Server. These examples are in .NET 8/.NET 6 and .NET Core 3.1.

Modify the Program.cs File

Here is an example snippet from the .NET 6/.NET 8 server's Program.cs file. See the comments in the code.

var app = builder.Build();


var provider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider();
// NOTE: Add new mappings
provider.Mappings[".gltf"] = "model/gltf+json"; // NOTE: add the extension (with period) and its type



// NOTE: comment this line of code out
// app.UseStaticFiles();


app.UseStaticFiles(new StaticFileOptions // NOTE: replace the line app.UseStaticFiles(); with this block of code
{
    ContentTypeProvider = provider
});

Make sure to add any newly added MIME-Types to the server's type of files that can be compressed assuming they are not already compressed formats. Read more here.

This article requires ASP.NET Core 3.1 even though it may work with earlier versions.

Modify the Startup.cs File

Here is the modified Startup.cs file. See the comments in the code.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.StaticFiles; // NOTE: this line of code added
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace ProjectName
{
	public class Startup
	{
		public Startup(IConfiguration configuration)
		{
			Configuration = configuration;
		}

		public IConfiguration Configuration { get; }

		public void ConfigureServices(IServiceCollection services)
		{
			services.AddControllersWithViews();
		}

		public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
		{

			if (env.IsDevelopment())
			{
				app.UseDeveloperExceptionPage();
			}
			else
			{
				app.UseExceptionHandler("/Home/Error");
			}



			// NOTE: this code added to the file
			var provider = new FileExtensionContentTypeProvider();
			// NOTE: Add new mappings
			provider.Mappings[".gltf"] = "model/gltf+json"; // NOTE: add the extension (with period) and its type



			// NOTE: comment this line of code out
			// app.UseStaticFiles();


			// NOTE: this code added to the file
			app.UseStaticFiles(new StaticFileOptions // NOTE: replace the line app.UseStaticFiles(); with this block of code
			{
				ContentTypeProvider = provider
			});




			app.UseRouting();

			app.UseAuthorization();

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

Make sure to add any newly added MIME-Types to the server's type of files that can be compressed assuming they are not already compressed formats. Read more here.


This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE