Skip to content

Instantly share code, notes, and snippets.

@hasmukhlalpatel
Last active March 14, 2025 13:29
Show Gist options
  • Select an option

  • Save hasmukhlalpatel/6eb8fff0cddae550c77f8834adf0511a to your computer and use it in GitHub Desktop.

Select an option

Save hasmukhlalpatel/6eb8fff0cddae550c77f8834adf0511a to your computer and use it in GitHub Desktop.
cors
    {
      "Cors": {
        "AllowedOrigins": [
          "http://localhost:3000"
        ],
        "AllowedMethods": [
          "GET",
          "POST"
        ],
        "AllowedHeaders": [
          "*"
        ]
      }
    }
app.UseCors(policyName);
    var corsOptions = builder.Configuration.GetSection("Cors").Get<CorsOptions>();
    builder.Services.AddCors(options =>
    {
        options.AddPolicy(policyName, policy =>
        {
            policy.WithOrigins(corsOptions.AllowedOrigins.ToArray());
            policy.WithMethods(corsOptions.AllowedMethods.ToArray());
            policy.WithHeaders(corsOptions.AllowedHeaders.ToArray());
        });
    });
using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
public class TimingMiddleware
{
private readonly RequestDelegate _next;
public TimingMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var stopwatch = Stopwatch.StartNew();
await _next(context);
stopwatch.Stop();
var elapsedMs = stopwatch.ElapsedMilliseconds;
// Log the time taken
context.Response.Headers["X-Response-Time-ms"] = elapsedMs.ToString();
Console.WriteLine($"Request took {elapsedMs} ms");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment