Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to navigate to an ASP.NET Core MVC controller in Blazor app?

I have DownloadController.cs to Controllers/DownloadController with the following method:

public async Task<ActionResult> DownloadFile(string key) 
{
    return File(...);
}

Also, in my Startup.cs I have configured the following endpoints:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default".
        pattern: "{controller}/{action}");
    endpoints.MapControllers();
    endpoints.MapBlazorHub();
    endpoints.MapFallbackToPage("/_Host");
});

So how in a Blazor view can I navigate to the controller action? I was looking for something similar to this:

@Html.ActionLink(...);
like image 754
Adam Avatar asked Jan 09 '20 04:01

Adam


People also ask

Can you use MVC with Blazor?

Blazor applications are component-based. Blazor components can be used in existing ASP.NET MVC applications.

How do I navigate to another page in Blazor?

You can redirect to a page in Blazor using the Navigation Manager's NavigateTo method. In the following code snippet, it will redirect to the home page when this page gets loaded. Similarly, you can call NavigateTo() method from NavigationManager class anywhere to redirect to another page.


1 Answers

This should do:

@page "/MvcLinkExample"
@inject NavigationManager NavigationManager

<button @onclick="NavigateToMvcPage">MVC Link</button>

@code {
    private void NavigateToMvcPage()
    {
        NavigationManager.NavigateTo("controllername/actionname/10", true);
    }
}
like image 78
Kyle Avatar answered Oct 18 '22 03:10

Kyle