问题描述
我可能在这里遗漏了一些东西,因为我是 F# 新手,但是,我需要以下内容:
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"
网址:https://localhost:5001/my/ip
应返回:192.168.199.2
。
我收到的错误消息:
{"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."]}}
解决方法
我对 ASP.NET Core 没有太多经验,但我认为问题在于您在类和成员级别都设置了路由。这些是附加的,因此您的 Ip
操作的实际 URL 当前为 https://localhost:5001/my/my/ip
。
要解决此问题,请从类级别完全删除 Route
属性,或从成员级别路由中删除 [controller]
前缀:
[<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"