While my server-side Blazor app is running, I want some Javascript code in _Host.cshtml
to be able to post data to a controller action. Of course, this happens completely outside of the scope of, and is unrelated to, the Blazor app itself.
I thought this would be a matter of adding calls to services.AddControllers()
and endpoints.MapControllers()
at the appropriate places in Startup.cs
. However, after doing this, and implementing the controller action, I made the following observations:
@Url.Action
on the controller action returns a blank stringHow can I add controller (not view) support to my server-side Blazor project in a way that overcomes the above two issues?
If all you are ever doing is rendering pages server-side and interacting with them, then you can do this entirely through Blazor, with no need for controllers.
Blazor applications are component-based. Blazor components can be used in existing ASP.NET MVC applications.
Use: endpoints.MapControllers()
You can have this in your startup.cs:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
This controller:
[Route("api/[controller]")]
[ApiController]
public class DownloadController : Controller
{
private readonly IWebHostEnvironment environment;
public DownloadController(IWebHostEnvironment environment)
{
this.environment = environment;
}
[HttpGet("[action]")]
public IActionResult DownloadFile(string FileName)
{
string path = Path.Combine(
environment.WebRootPath,
"files",
FileName);
var stream = new FileStream(path, FileMode.Open);
var result = new FileStreamResult(stream, "text/plain");
result.FileDownloadName = FileName;
return result;
}
}
And this in your .razor page:
@inject NavigationManager NavigationManager
<button @onclick="DownloadFile">Download</button>
@code {
public void DownloadFile()
{
NavigationManager.NavigateTo($"/api/Download/DownloadFile?FileName=BlazorHelpWebsite.zip", true);
}
}
See: https://github.com/ADefWebserver/Blazor-Blogs/tree/master/BlazorBlogs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With