Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Error at app startup: An action cannot use both form and JSON body parameters

$
0
0

In a new Blazor WebApp project I'm converting as much C# as I can get away with, so that I can build as much of the application as possible in my preferred language. This includes pretty much everything in Program.cs.

Trouble spot: near the end of Program.cs (with Identity and Sample Pages scaffolded in) we find this call:

app.MapAdditionalIdentityEndpoints();

I've successfully converted everything in that extension method to VB.NET, and it builds, but I'm hitting a snag at runtime. The problem centers around this lambda:

accountGroup.MapPost("/Logout", async (    ClaimsPrincipal user,    SignInManager<User> signInManager,    [FromForm] string returnUrl) =>{    await signInManager.SignOutAsync();    return TypedResults.LocalRedirect($"~/{returnUrl}");});

Because VB.NET doesn't support attributes on lambda parameters (e.g. <FromForm>), I've had to expand that:

Public Module Extensions  Private Delegate Function LogoutDelegate(    User As ClaimsPrincipal,    SignInManager As SignInManager(Of ApplicationUser),<FromForm> ReturnUrl As String) As Task(Of RedirectHttpResult)<Extension>  Public Function MapSomeAdditionalIdentityEndpoints(Endpoints As IEndpointRouteBuilder) As IEndpointConventionBuilder    Dim oLogoutHandler As LogoutDelegate    Dim oAccountGroup As RouteGroupBuilder    oLogoutHandler = AddressOf LogoutAsync    oAccountGroup = Endpoints.MapGroup("/Account")    oAccountGroup.MapPost("/Logout", oLogoutHandler)    Return oAccountGroup  End Function  Private Async Function LogoutAsync(    User As ClaimsPrincipal,    SignInManager As SignInManager(Of ApplicationUser),<FromForm> ReturnUrl As String) As Task(Of RedirectHttpResult)    Await SignInManager.SignOutAsync()    Return TypedResults.LocalRedirect($"~/{ReturnUrl}")  End FunctionEnd Module

Yes, it's a verbose mess, but oh well. Once it's done and working I'll figure out a way to trim it all down a bit.

When I use this signature, the app crashes at startup (app.Run). Here's the full error message:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dllAn action cannot use both form and JSON body parameters.Below is the list of parameters that we found:Parameter           | Source---------------------------------------------------------------------------------`SignInManager       | Body (Inferred)ReturnUrl           | Form (Attribute)

This would seem to make sense since the SignInManager parameter isn't specifically decorated. However, when I alter the signature slightly and add the <FromForm> attribute, like this:

<FromForm> SignInManager As SignInManager(Of ApplicationUser)

...the app does run.

The problem with that, though, is that SignInManager is null when the delegate function is called. It's also null when I use <FromServices>, even though the SignInManager is being registered early on:

builder.Services.AddIdentityCore<User>(options => options.SignIn.RequireConfirmedAccount = true)    .AddEntityFrameworkStores<Context>()    .AddSignInManager()    .AddDefaultTokenProviders();

None of this behavior manifests when I'm running the C# version of the lambda.

I have no idea where to begin looking for a solution to this. I'm throwing weird stuff at the wall just to see if it sticks. I've tried all of the From* attributes, I've tried removing <FromForm> from the ReturnUrl parameter on both the delegate and the function, respectively.

It crashes with the decoration on the delegate but not the function, and it runs with the decoration on the function but not the delegate. But with the latter I get a 415 error when I click the Logout button. And a 415 when I remove both decorations.

Changing ReturnUrl to <FromBody>, to align with the inference for SignInManager, also causes a crash, albeit with a bit different error:

Failure to infer one or more parameters.Below is the list of parameters that we found: Parameter           | Source                        ---------------------------------------------------------------------------------SignInManager       | Body (Inferred)ReturnUrl           | UNKNOWN        Did you mean to register the "UNKNOWN" parameters as a Service?

None of this behavior manifests with the C# version.

Searching for answers on this has proven pointless, given the factors involved. My scenario is pretty rare, I think.

I'm new to Blazor; I'm not sure what's going on here internally. Why is the inference causing a problem in my VB.NET version, but not the C# version? How can I get this code to run?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>