Sup*_*JMN 5 .net c# asp.net-web-api refit
我的Web API中的Get方法遇到问题:API获取对象但具有默认值。
例如:
myRefitClient.GetSummary(new MyClass() { Prop = 1 });
Run Code Online (Sandbox Code Playgroud)
Web API正确接收MyClass实例,但Prop为0!
这就是我所拥有的:
Get方法(Web API中的Controller):
[HttpGet]
async Task<ActionResult> Get([FromQuery]MyClass req)
Run Code Online (Sandbox Code Playgroud)
MyClass是:
public class MyClass
{
public int Prop { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的Web API Refit接口是:
public interface IMyWebApi
{
[Get("/api/mycontroller")]
Task<PositionSummary> GetSummary(MyClass req);
}
Run Code Online (Sandbox Code Playgroud)
因此,正如我所说,在致电时:
service.GetSummary(new MyClass() { Prop = 1 });
Run Code Online (Sandbox Code Playgroud)
我MyClass在控制器中得到一个实例,但Prop为0,而不是1。
我究竟做错了什么?
To force ASP.NET Web API (not ASP.NET Core) to read a complex type from the URI, add the [FromUri] attribute to the parameter:
[HttpGet]
public async Task<ActionResult> Get([FromUri]MyClass req)
Run Code Online (Sandbox Code Playgroud)
If you are using ASP.NET Core then use [FromQuery] attribute
[HttpGet]
public async Task<ActionResult> Get([FromQuery]MyClass req)
Run Code Online (Sandbox Code Playgroud)
I'm getting a MyClass Instance in my Controller, but Prop is 0, instead of 1.
If /api/mycontroller is set as launchUrl in launchSettings.json then you will get MyClass Instance with Prop = 0 at the start of application. But next call using Refit should pass parameters