julazor:如何将多个参数从navigateto传递到Web API控制器以下载文件

发布于 2025-02-07 08:45:14 字数 1828 浏览 2 评论 0 原文

我正在尝试在Flazor中使用 Navivgateto 传递文件ID和名称以从我的下载控制器下载文件。

什么是正确的设置?我已经尝试了许多可能性,但我一直看到一个错误:对不起,此地址没有什么。

Razor Page

        public async Task SelectedDisplayDbItemChanged(DisplayDbItemsComboBoxItemDTO item)
        {
            Data = null;
            
            Data = GetDataTable();

            var fileId = await utilities.ExportDataTableToFile((DataTable)Data).ConfigureAwait(false);
            //navigationManager.NavigateTo($"api/download/fileId/" + fileId + "/fileName/" + "myfile", true);
            //?data1=678&data2=c-sharpcorner
            navigationManager.NavigateTo($"api/Download/{fileId}/{"myfile"}", true);
        }

Controller:

        [HttpPost("Download/{fileId}/{fileName}")]
        public async Task<IActionResult> Download(string fileId, string fileName)
        {
            using (var ms = new MemoryStream())
            {
                var fullPath = Path.Combine(DownloadPath, fileId);
                await using (var stream = new FileStream(fullPath, FileMode.Open))
                {
                    await stream.CopyToAsync(ms);
                }
                ms.Position = 0;
                return File(ms, "application/octet-stream", $"{fileName}.xlsx");
            }
        }

我已经看到了很多从剃刀页到剃须刀页面的示例,但不是从 navigateto 到传递多个参数的控制器。

我也尝试了这些响应: https://stackover.com/a/a/a/71130256/9594249 https://stackoverflow.com/a/a/71130256/9594249

I'm trying to use NavivgateTo in Blazor to pass a file id and name to download a file from my Download controller.

What is the proper setup? I've tried a number of possibilities and I keep seeing an error: Sorry, there is nothing at this address.

Razor Page

        public async Task SelectedDisplayDbItemChanged(DisplayDbItemsComboBoxItemDTO item)
        {
            Data = null;
            
            Data = GetDataTable();

            var fileId = await utilities.ExportDataTableToFile((DataTable)Data).ConfigureAwait(false);
            //navigationManager.NavigateTo(
quot;api/download/fileId/" + fileId + "/fileName/" + "myfile", true);
            //?data1=678&data2=c-sharpcorner
            navigationManager.NavigateTo(
quot;api/Download/{fileId}/{"myfile"}", true);
        }

Controller:

        [HttpPost("Download/{fileId}/{fileName}")]
        public async Task<IActionResult> Download(string fileId, string fileName)
        {
            using (var ms = new MemoryStream())
            {
                var fullPath = Path.Combine(DownloadPath, fileId);
                await using (var stream = new FileStream(fullPath, FileMode.Open))
                {
                    await stream.CopyToAsync(ms);
                }
                ms.Position = 0;
                return File(ms, "application/octet-stream", 
quot;{fileName}.xlsx");
            }
        }

I've seen a lot of examples from the Razor page to the Razor page, but not from NavigateTo to a controller with passing multiple parameters.

enter image description here

I've tried these responses as well: https://stackoverflow.com/a/71130256/9594249
https://stackoverflow.com/a/71130256/9594249

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧夏天 2025-02-14 08:45:14

不像ASP.NET MVC或Razor Page,在[参数]标签中传递了Blazor参数,

@page "/Download/{fileId}/{fileName}"
@code {
    [Parameter]
    public string? fileId { get; set; }
    [Parameter]
    public string? fileName { get; set; }
}

请参考: https://learn.microsoft.com/en-us/aspnet/aspnet/core/core/blazor/fundamentals/routing?view?view = = appnetcore-6.0


添加到program.cs或startup.cs:

builder.Services.AddRazorPages(options => {
    options.Conventions.AddPageRoute("/DownloadPage", "Download/{fileId?}/{fileName?}");
    }
});

pages/downloadpage.cshtml

@page "{fileId?}/{fileName?}"
@model BlazorApp.Pages.DownloadModel

pages/downloadpage.cshtml.cs,

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace BlazorApp.Pages;

    public class DownloadModel : PageModel
    {
         private readonly IWebHostEnvironment _env;
    
         public DownloadModel(IWebHostEnvironment env)
         {
             _env = env;
         }
        public IActionResult OnGet()
        {
            // work with RouteData.Values["fileId"] and RouteData.Values["fileName"]
        }
    }

请参阅:

https://learn.microsoft.com/en-us/answers/questions/243420/blazor-server-app- downlaod-files-from-server.html

https://learn.microsoft.com/ko-kr/aspnet/core/razor-pages/razor-pages/razor-pages-conventions?view= aspnetcore-6.0

Not like Asp.net MVC or razor page, in Blazor parameters are passed by [Parameter] tag

@page "/Download/{fileId}/{fileName}"
@code {
    [Parameter]
    public string? fileId { get; set; }
    [Parameter]
    public string? fileName { get; set; }
}

please refer : https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/routing?view=aspnetcore-6.0


(Updated)

add to Program.cs or Startup.cs:

builder.Services.AddRazorPages(options => {
    options.Conventions.AddPageRoute("/DownloadPage", "Download/{fileId?}/{fileName?}");
    }
});

Pages/DownloadPage.cshtml

@page "{fileId?}/{fileName?}"
@model BlazorApp.Pages.DownloadModel

Pages/DownloadPage.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace BlazorApp.Pages;

    public class DownloadModel : PageModel
    {
         private readonly IWebHostEnvironment _env;
    
         public DownloadModel(IWebHostEnvironment env)
         {
             _env = env;
         }
        public IActionResult OnGet()
        {
            // work with RouteData.Values["fileId"] and RouteData.Values["fileName"]
        }
    }

please refer :

https://learn.microsoft.com/en-us/answers/questions/243420/blazor-server-app-downlaod-files-from-server.html

https://learn.microsoft.com/ko-kr/aspnet/core/razor-pages/razor-pages-conventions?view=aspnetcore-6.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文