C
C#•8mo ago
schwartzmj

Redirect after form POST Blazor Static

Am I not able to have a statically rendered Blazor @code block return a redirect? e.g.:
c#
@code {

private sealed class InputModel
{
[Required]
[MaxLength(50)] public string Name { get; set; } = "";
[Required]
[MaxLength(300)]
public string Description { get; set; } = "";
}

[SupplyParameterFromForm] private InputModel Input { get; set; } = new();

public async Task CreateWebsite()
{
try
{
var website = new Website
{
Name = Input.Name,
Description = Input.Description
};
DbContext.Websites.Add(website);
await DbContext.SaveChangesAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
}

}
}
c#
@code {

private sealed class InputModel
{
[Required]
[MaxLength(50)] public string Name { get; set; } = "";
[Required]
[MaxLength(300)]
public string Description { get; set; } = "";
}

[SupplyParameterFromForm] private InputModel Input { get; set; } = new();

public async Task CreateWebsite()
{
try
{
var website = new Website
{
Name = Input.Name,
Description = Input.Description
};
DbContext.Websites.Add(website);
await DbContext.SaveChangesAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
}

}
}
7 Replies
schwartzmj
schwartzmjOP•8mo ago
I get NavigationManager errors if I try to use NavigationManager. My understanding is that might make sense because that's supposed to be used client side / with JavaScript.
D.Mentia
D.Mentia•8mo ago
I don't see a redirect there... but NavigationManager should be how you do it either way, as far as I know
schwartzmj
schwartzmjOP•8mo ago
I get the error
Microsoft.AspNetCore.Components.NavigationException: Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown.
at Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager.NavigateToCore(String uri, NavigationOptions options)
at Microsoft.AspNetCore.Components.NavigationManager.NavigateToCore(String uri, Boolean forceLoad)
at Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(String uri, Boolean forceLoad, Boolean replace)
at WebsiteBuilder.Components.Websites.Create.CreateWebsite()
Microsoft.AspNetCore.Components.NavigationException: Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown.
at Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager.NavigateToCore(String uri, NavigationOptions options)
at Microsoft.AspNetCore.Components.NavigationManager.NavigateToCore(String uri, Boolean forceLoad)
at Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(String uri, Boolean forceLoad, Boolean replace)
at WebsiteBuilder.Components.Websites.Create.CreateWebsite()
by adding:
@inject NavigationManager NavigationManager
@inject NavigationManager NavigationManager
c#
NavigationManager.NavigateTo("/websites");
c#
NavigationManager.NavigateTo("/websites");
under await DbContext.SaveChangesAsync();
D.Mentia
D.Mentia•8mo ago
is this blazor client?
schwartzmj
schwartzmjOP•8mo ago
It's a statically rendered page I'm looking to get Razor Pages like functionality for forms
D.Mentia
D.Mentia•8mo ago
No idea then, assumedly the blazor stuff registers that NavigationManager and it may not be registered for you or something it needs isn't setup right, etc, since that doesn't look like a registration error
schwartzmj
schwartzmjOP•8mo ago
if i change the rendering mode, it works. but it will only work with javascript enabled (essentially becoming a SPA) i assumed i could have the browser receive a redirect response from the post request like a normal page cant seem to use methods like RedirectToPage in a @code { } block or blazor code behind file @D.Mentia figured it out 😎 . The NavigateTo method throws if the page is statically rendered. this exception is meant to get caught by the framework to handle the redirect my current code catches the error and does nothing with it. moving it outside of the try/catch works
Want results from more Discord servers?
Add your server