Skip to content

Instantly share code, notes, and snippets.

@musukvl
Created December 26, 2018 17:37
Show Gist options
  • Save musukvl/24e7c76bb4cd60ba55a405aacd49ff17 to your computer and use it in GitHub Desktop.
Save musukvl/24e7c76bb4cd60ba55a405aacd49ff17 to your computer and use it in GitHub Desktop.
ASP.NET Core Hello Word with SampleMiddleware
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace WebApplication1
{
public class Program
{
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMiddleware<SampleMiddleware>();
app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); });
}
}
public class SampleMiddleware
{
private readonly RequestDelegate _next;
public SampleMiddleware(RequestDelegate next)
{
this._next = next;
}
public async Task Invoke(HttpContext context)
{
Console.WriteLine($"{context.Request.Path}{context.Request.QueryString}");
await this._next(context);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment