Ed Barnes
Ed Barnes

Reputation: 41

Razor Page Redirect to Razor Component

I have the following setup in my project:

/Pages/Page1.razor
/Pages/Page2.cshtml

Page 1 is a Blazor component.

Page 2 is a Razor Page.

At the top of Page1:

@page "/Page1"

In the Page2.cshtml.cs code-behind I have:

public IActionResult OnPostAddNew()
{
    return RedirectToPage("Page1");
}

I have tried everything I can think of but I always get:

InvalidOperationException: No page named 'Page1' matches the supplied values.

I appreciate that Page1 is not technically a page but I haven't been able to figure out how to redirect. A NavLink works just fine to find Page1.

Upvotes: 3

Views: 1554

Answers (1)

mhenrickson
mhenrickson

Reputation: 656

I was having the same problem redirecting off my Logout page. Just using Redirect instead of RedirectToPage worked for me. RedirectToPage got me the same error as you. In my example the "Counter" page is Counter.razor page in the sample code you get when create a Blazor project in Visual Studio.

public async Task<IActionResult> OnGet()
{
    await _signInManager.SignOutAsync();
    return Redirect("~/Counter");
}

LocalRedirect also appears to work:

return LocalRedirect(Url.Content("~/Counter"));

Upvotes: 5

Related Questions