Last active
October 3, 2025 11:52
-
-
Save jchannon/1f9335f3929a41a378213bd22940ab3b to your computer and use it in GitHub Desktop.
Testing CSP form-action:self with Blazor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @page "/testform" | |
| @using Microsoft.Extensions.Logging | |
| @using System.ComponentModel.DataAnnotations | |
| @inject NavigationManager Navigation | |
| @inject LinkGenerator LinkGenerator | |
| @inject ILogger<TestForm> Logger | |
| <EditForm Model="Model" OnSubmit="Submit" FormName="OrderCharger"> | |
| <label> | |
| Identifier: | |
| <InputText @bind-Value="Model!.Id"/> | |
| </label> | |
| <button class="">Submit</button> | |
| @if (Submitted) | |
| { | |
| <p>Submitted!!!</p> | |
| } | |
| </EditForm> | |
| @code { | |
| [SupplyParameterFromForm] | |
| private Starship? Model { get; set; } | |
| [CascadingParameter] | |
| private HttpContext? HttpContext { get; set; } | |
| public bool Submitted { get; set; } | |
| [Inject] | |
| private IJSRuntime JS { get; set; } = default!; | |
| protected override void OnInitialized() | |
| { | |
| Model ??= new(); | |
| Submitted = false; | |
| } | |
| private void Submit() | |
| { | |
| Submitted = true; | |
| Logger.LogInformation("Id = {Id}", Model?.Id); | |
| Navigation.NavigateTo("/foo"); | |
| //Navigation.NavigateTo(initializeOrderUrl, true); | |
| //HttpContext?.Response.Redirect(initializeOrderUrl, permanent: false); | |
| //await JS.InvokeVoidAsync("window.location.href", initializeOrderUrl); | |
| } | |
| public class Starship | |
| { | |
| [Required] | |
| [StringLength(10, ErrorMessage = "Id is too long.")] | |
| public string? Id { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment