I use this link from microsoft https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0 to create my web api.
after that, I add the database like this:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<Context>(opt => opt.UseSqlServer(
builder.Configuration.GetConnectionString("Default")
));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
var app = builder.Build();
and my functions are like this:
app.MapGet("/SomeRoute/{data}", async (long data, Context appDb) =>
{
var tabladata = await appDb.Table1
.Where(some code here)
.ToListAsync();
return Results.Ok(tabladata);
});
And I found that I can get the IP client with this code: add this before var app = builder.Build();
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
and this after:
app.UseForwardedHeaders();
and it's looks like this in the end:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<Context>(opt => opt.UseSqlServer(
builder.Configuration.GetConnectionString("Default")
));
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
var app = builder.Build();
app.UseForwardedHeaders();
how I gonna get the IP Client that call the web api functions?
Thanks.