简体   繁体   中英

How can I map controller endpoints to actions on F#?

I am probably missing something here as I am new to F#, however, I need the following:

open Microsoft.AspNetCore.Mvc


[<ApiController>]
[<Route("[controller]")>]
type MyController () =
    inherit ControllerBase()

    
    //[<HttpGet(Name = "Ip")>] doesn't work neither.
    [<HttpGet>]
    [<Route("[controller]/[action]")>]
    member _.Ip() =
        "192.168.199.2"

The URL: https://localhost:5001/my/ip should return: 192.168.199.2 .

The error message I am getting instead:

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"00-389e8d2f6bc3a342a3754b5c5ce7915f-7e6e851c78f47c4f-00","errors":{"id":["The value 'ip' is not valid."]}}

I don't have much experience with ASP.NET Core, but I think the problem is that you have a route set at both the class and member level. These are additive, so the actual URL of your Ip action is currently https://localhost:5001/my/my/ip .

To fix this, remove the Route attribute entirely from the class level, or remove the [controller] prefix from your member-level route:

[<ApiController>]
[<Route("[controller]")>]   // controller is specified here, so...
type MyController() =
    inherit ControllerBase()

    [<HttpGet>]
    [<Route("[action]")>]   // ...no controller specified here
    member _.Ip() =
        "192.168.199.2"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM