dotnet / dotnet/aspnetcore

Minimal API F# RouteValue is Null

Open
#38,906 4 comments 0 reactions 1 assignee Claimed by @halter73 View on GitHub
area-minimal
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

Mapping a route with a route value in F# fails to bind the value when using a named function instead of a anonymous function. I'm not sure if this is an ASP.NET or F# issue (or maybe just a consequence of how F# works with the delegate param).

`dotnet new web -lang F#`

```Program.fs
open System
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.Hosting

[]
let main args =
let builder = WebApplication.CreateBuilder(args)
let app = builder.Build()

let funcB v = v
let funcC = fun v -> v

app.MapGet("/a/{v}", Func (fun v -> v)) |> ignore
app.MapGet("/b/{v}", Func funcB) |> ignore
app.MapGet("/c/{v}", Func funcC) |> ignore

app.Run()

0 // Exit code

```

`dotnet run`

```
GET /a/test

test
```

```
GET /b/test

System.ArgumentNullException: Value cannot be null. (Parameter 'text')
at Microsoft.AspNetCore.Http.HttpResponseWritingExtensions.WriteAsync(HttpResponse response, String text, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.ExecuteWriteStringResponseAsync(HttpContext httpContext, String text)
at lambda_method2(Closure , Object , HttpContext )
at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass36_0.b__0(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
```

```
GET /c/test

System.ArgumentNullException: Value cannot be null. (Parameter 'text')
at Microsoft.AspNetCore.Http.HttpResponseWritingExtensions.WriteAsync(HttpResponse response, String text, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.ExecuteWriteStringResponseAsync(HttpContext httpContext, String text)
at lambda_method3(Closure , Object , HttpContext )
at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass36_0.b__0(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
```

```
$ dotnet --info
.NET SDK (reflecting any global.json):
Version: 6.0.100
Commit: 9e8b04bbff

Runtime Environment:
OS Name: arch
OS Version:
OS Platform: Linux
RID: arch-x64
Base Path: /usr/share/dotnet/sdk/6.0.100/

Host (useful for support):
Version: 6.0.0
Commit: 4822e3c3aa

.NET SDKs installed:
6.0.100 [/usr/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.