C
C#9mo ago
porksausages

❔ ASP.NET Core WebAPI Dynamic route mapping at runtime with path parameters

For context, the application I'm working on receives configuration from an external source, and then spins up a WebApi based on that configuration. This works fine with simple definitions, but I’ve hit a brick wall trying to figure out if/how path parameters would be possible at runtime. At the moment I’m currently doing something like this to create the endpoints:
foreach (var endpoint in endpoints)
{
var parameters = ParameterParser.Parse(endpoint.Query).ToList();

app.MapPost(
endpoint.Path,
async (HttpResponse response, [FromBody] JsonElement json) =>
{
parameters.ForEach(x => x.Value = GetJsonValue(x, json.GetProperty(x.Name)));

// do stuff

response.StatusCode = 200;
});
}
foreach (var endpoint in endpoints)
{
var parameters = ParameterParser.Parse(endpoint.Query).ToList();

app.MapPost(
endpoint.Path,
async (HttpResponse response, [FromBody] JsonElement json) =>
{
parameters.ForEach(x => x.Value = GetJsonValue(x, json.GetProperty(x.Name)));

// do stuff

response.StatusCode = 200;
});
}
But I have no idea if I could build the handler function, which may have any number of parameters, dynamically. I know it's possible to create DynamicMethods, but I assume the compiler would need to know the signature of the handler at compile time, which would make it impossible to do this in the way that I'm trying to do it. If that assumption is correct, is there an alternative way to achieve this?
1 Reply
Accord
Accord9mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.