AspNetCore - HTTP

StatusCode:
Here's the complete code for a basic ASP.NET Core application, along with a description:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Run(async (HttpContext context) =>
{
if (1 == 1)
{
context.Response.StatusCode = 200;
}
else
{
context.Response.StatusCode = 400;
}
await context.Response.WriteAsync("Hello");
await context.Response.WriteAsync(" World");
});
/*
context.Response.Headers["Content-Type"] = "text/plain";
await context.Response.WriteAsync("<h1>Hello</h1>");
await context.Response.WriteAsync("<p>World</p>");
await context.Response.WriteAsync("Hello");
await context.Response.WriteAsync(" World");
*/
app.Run();
Description:
This code sets up a minimal ASP.NET Core application using the WebApplication
class. It begins by creating a web application builder with WebApplication.CreateBuilder(args)
, which is responsible for configuring the application. The builder.Build
()
method then constructs the application.
The application defines a single endpoint using app.Run
()
, which is a method that configures the HTTP request pipeline. This endpoint sets the HTTP response status code to 200, indicating a successful request. It then writes "Hello World" to the HTTP response body using context.Response.WriteAsync("Hello World")
.
Finally, the application is started with the app.Run
();
call, which listens for incoming HTTP requests and processes them using the defined pipeline. This example is useful for understanding the basic structure of an ASP.NET Core application and how to handle HTTP requests and responses.
Response Header
This code snippet demonstrates how to set up a basic ASP.NET Core application that customizes HTTP response headers and writes HTML content to the response body:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Run(async context =>
{
context.Response.Headers["MyKey"] = "my value";
context.Response.Headers["Server"] = "My server";
context.Response.Headers["Content-Type"] = "text/html";
await context.Response.WriteAsync("<h1>Hello</h1>");
await context.Response.WriteAsync("<h2>World</h2>");
});
app.Run();
Description:
This ASP.NET Core application is configured using the WebApplication
class. It starts by creating a builder and building the application. The application defines a single endpoint using app.Run
()
, which sets custom HTTP response headers. Specifically, it adds a custom header "MyKey" with the value "my value", overrides the "Server" header with "My server", and sets the "Content-Type" to "text/html" to indicate that the response contains HTML content.
The endpoint then writes HTML content to the response body, displaying "Hello" as an <h1>
heading and "World" as an <h2>
heading. The application is started with the app.Run
();
call, which listens for incoming HTTP requests and processes them using the defined pipeline. This example illustrates how to manipulate HTTP headers and serve HTML content in an ASP.NET Core application.
Http-ReQuest
This code snippet demonstrates how to set up an ASP.NET Core application that responds with the request path and method in the HTTP response body:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Run(async context =>
{
string path = context.Request.Path;
string method = context.Request.Method;
context.Response.Headers["Content-type"] = "text/html";
await context.Response.WriteAsync($"<p>{path}</p>");
await context.Response.WriteAsync($"<p>{method}</p>");
});
app.Run();
Description:
This ASP.NET Core application is configured using the WebApplication
class. It starts by creating a builder and building the application. The application defines a single endpoint using app.Run
()
, which processes incoming HTTP requests.
Within the endpoint, the request path and method are retrieved from the HttpContext
object. The response header "Content-type" is set to "text/html" to indicate that the response contains HTML content. The application then writes the request path and method to the response body as HTML paragraphs.
The application is started with the app.Run
();
call, which listens for incoming HTTP requests and processes them using the defined pipeline. This example illustrates how to access request information and return it in the response in an ASP.NET Core application.
QueryString
This code snippet demonstrates how to set up an ASP.NET Core application that responds with the request path and method in the HTTP response body:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Run(async (HttpContext context) =>
{
context.Response.Headers["Content-type"] = "text/html";
if (context.Request.Method == "GET")
{
if (context.Request.Query.ContainsKey("id"))
{
string id = context.Request.Query["id"];
await context.Response.WriteAsync($"<p>{id}</p>");
}
}
});
app.Run();
Description:
This ASP.NET Core application is configured using the WebApplication
class. It starts by creating a builder and building the application. The application defines a single endpoint using app.Run
()
, which processes incoming HTTP requests.
Within the endpoint, the request path and method are retrieved from the HttpContext
object. The response header "Content-type" is set to "text/html" to indicate that the response contains HTML content. The application then writes the request path and method to the response body as HTML paragraphs.
The application is started with the app.Run
();
call, which listens for incoming HTTP requests and processes them using the defined pipeline. This example illustrates how to access request information and return it in the response in an ASP.NET Core application.
Http Request Header
This code snippet demonstrates how to set up an ASP.NET Core application that responds with the "User-Agent" header from the HTTP request:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Run(async context =>
{
context.Response.Headers["Content-type"] = "text/html";
if (context.Request.Headers.ContainsKey("User-Agent"))
{
string userAgent = context.Request.Headers["User-Agent"];
await context.Response.WriteAsync($"<p>{userAgent}</p>");
}
});
app.Run();
Description:
This ASP.NET Core application is configured using the WebApplication
class. It starts by creating a builder and building the application. The application defines a single endpoint using app.Run
()
, which processes incoming HTTP requests.
Within the endpoint, the response header "Content-type" is set to "text/html" to indicate that the response contains HTML content. The application checks if the "User-Agent" header is present in the request headers. If it is, the value of the "User-Agent" header is retrieved and written to the response body as an HTML paragraph.
The application is started with the app.Run
();
call, which listens for incoming HTTP requests and processes them using the defined pipeline. This example illustrates how to access and display request headers in an ASP.NET Core application.
Postman
This code snippet demonstrates how to set up an ASP.NET Core application that responds with the "AuthorizationKey" header from the HTTP request:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Run(async context =>
{
context.Response.Headers["Content-type"] = "text/html";
if (context.Request.Headers.ContainsKey("AuthorizationKey"))
{
string auth = context.Request.Headers["AuthorizationKey"];
await context.Response.WriteAsync($"<p>{auth}</p>");
}
});
app.Run();
Description:
This ASP.NET Core application is configured using the WebApplication
class. It starts by creating a builder and building the application. The application defines a single endpoint using app.Run
()
, which processes incoming HTTP requests.
Within the endpoint, the response header "Content-type" is set to "text/html" to indicate that the response contains HTML content. The application checks if the "AuthorizationKey" header is present in the request headers. If it is, the value of the "AuthorizationKey" header is retrieved and written to the response body as an HTML paragraph.
The application is started with the app.Run
();
call, which listens for incoming HTTP requests and processes them using the defined pipeline. This example illustrates how to access and display specific request headers in an ASP.NET Core application.
Http Get vs Post
This code snippet demonstrates how to set up an ASP.NET Core application that reads and processes the request body as a query string:
using Microsoft.Extensions.Primitives;
using System.IO;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Run(async context =>
{
StreamReader reader = new StreamReader(context.Request.Body);
string body = await reader.ReadToEndAsync();
Dictionary<string, StringValues> queryDict = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(body);
if (queryDict.ContainsKey("firstName"))
{
string firstName = queryDict["firstName"][0];
await context.Response.WriteAsync(firstName);
}
});
app.Run();
Description:
This ASP.NET Core application is configured using the WebApplication
class. It starts by creating a builder and building the application. The application defines a single endpoint using app.Run
()
, which processes incoming HTTP requests.
Within the endpoint, the request body is read as a stream using a StreamReader
. The body content is then parsed as a query string using QueryHelpers.ParseQuery
, which converts it into a dictionary of key-value pairs.
The application checks if the "firstName" key is present in the parsed query dictionary. If it is, the value associated with "firstName" is retrieved and written to the response body.
The application is started with the app.Run
();
call, which listens for incoming HTTP requests and processes them using the defined pipeline. This example illustrates how to read and parse the request body as a query string in an ASP.NET Core application.
Subscribe to my newsletter
Read articles from Md Asif Alam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Md Asif Alam
Md Asif Alam
๐ Full Stack .NET Developer & React Enthusiast ๐จโ๐ป About Me: With 3+ years of experience, I'm passionate about crafting robust solutions and seamless user experiences through code. ๐ผ Expertise: Proficient in .NET Core API, ASP.NET MVC, React.js, and SQL. Skilled in backend architecture, RESTful APIs, and frontend development. ๐ Achievements: Led projects enhancing scalability by 50%, delivered ahead of schedule, and contributed to open-source initiatives. ๐ Future Focus: Eager to embrace new technologies and drive innovation in software development. ๐ซ Let's Connect: Open to new opportunities and collaborations. Reach me on LinkedIn or GitHub!