Как в Net 6 из middleware перенаправить вызов на endpoint?

Рейтинг: 0Ответов: 1Опубликовано: 05.03.2023

Проект был написан на Net Core 2.2 и такая конструкция работала:

    public static void ProcessFileStorageRequest(this HttpRequest request, string path)
    {
        path = path.TrimEnd(new[] { '/' });
        request.Path = "/productfile/download";
        string encodedPath = UrlEncoder.Default.Encode(path);
        request.QueryString = new QueryString($"?path={encodedPath}");
    }

Теперь метод download не вызывается. Url меняется с https://localhost:5000/path/download.html на такой https://localhost:5000/path/file.exe/?path=path%2Ffile.exe

request изменяется, прилагаю скрин из дебага:

введите сюда описание изображения

Ответы

▲ 0Принят

Нашёл для себя решение:

public static void ProcessFileStorageRequest(this HttpContext context, string path, EndpointDataSource endpointDataSource)
{
    var endpoint = endpointDataSource.Endpoints.FirstOrDefault(e =>
    {
         var descriptor = e.Metadata.GetMetadata<ControllerActionDescriptor>();

         return descriptor != null
                       && "download".Equals(descriptor.ActionName, StringComparison.OrdinalIgnoreCase)
                       && "productfile".Equals(descriptor.ControllerName, StringComparison.OrdinalIgnoreCase);
    });

    if (endpoint == null)
    {
         throw new Exception("No valid endpoint found.");
    }

    context.SetEndpoint(endpoint);
}