C
C#2y ago
int

✅ Respond with HTML instead of plaintext

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

app.MapGet("/", () => "<h1>Hello World!</h1>");

app.Run();
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "<h1>Hello World!</h1>");

app.Run();
This is my entire app right now - how do I make the entire app use HTML as the content type by default?
5 Replies
cap5lut
cap5lut2y ago
int
intOP2y ago
yeah but most of my app will return HTML responses wouldn't that be tiring to do for each one can i set a default for it
cap5lut
cap5lut2y ago
the easiest would be to use something like MVC, razor pages or blazor. minimal apis is mainly designed for web apis
blueberriesiftheywerecats
Add middleware after creating app variable that making your response as html
app.Use(async (context, next) =>
{
context.Response.ContentType = "text/html";
await next(context);
});
app.Use(async (context, next) =>
{
context.Response.ContentType = "text/html";
await next(context);
});
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?