0

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($"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"); } } 

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

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is calling a web api controller from within Blazor. Maybe I'm doing it all wrong (new to Blazor), but I need to open up another tab to download a file. My controller to download the file works good if I add in a static file id, file name.
Ah, now I see the point now. Blazor has no controller like ASP.net MVC so it will be complicated to do so and I think you have to work with startup.cs or program.cs. I'll update my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.