I need to intercept GET requests in ASP.NET Core 3 and, if the request match some conditions, I need to execute an Action in a Controller (or a Page). Always the same action.
For example if the GET request match one of these:
\page1
\page2
\page3
\abc
\def
I need to execute the Controllers\CustomController.CustomAction and pass the request information to the controller Action.
I tried to create a CustomEndpointRouteBuilderExtensions following the guidance in Microsoft Docs Routing:
app.UseEndpoints(endpoints =>
{
endpoints.MapCustom();
});
For the CustomEndpointRouteBuilderExtensions class I think I have to write a custom EndpointDataSource that contains all my dictionary of routes and the corresponding RequestDelegate that points to my CustomAction.
I followed the FrameworkEndpointDataSource sample but it uses the RequestDelegate writted directly on Startup.cs.
How can I create an Endpoint to add to the CustomDataSource?
Thanks in advance!
Update 1
I've found another way: I change the request before the UseRouting():
app.UseCustomEngine();
app.UseRouting();
public static IApplicationBuilder UseCustomEngine(this IApplicationBuilder builder)
{
builder.Use(async (context, next) =>
{
if (dict.ContainsKey(context.Request.Path))
{
context.Request.Path = PathString.FromUriComponent("/CustomController/CustomAction");
context.Request.QueryString = new QueryString($"?key=123");
}
await next.Invoke();
});
return builder;
}
What do you think? It's a good solution?