在AspNetCore 2.2中的ApiController单个操作中使用FromRoute和FromQuery

时间:2019-05-03 11:31:39

标签: c# asp.net-core asp.net-core-mvc asp.net-core-2.2

使用ASP.NET Core 2.2,我可以执行以下ApiController操作:

[ApiController]
public class PostController : Controller 
{

    [HttpGet("posts/{postId:int:min(1)}")]
    public async Task<IActionResult> GetByPostId([FromQuery]GetByPostIdRequest request) 
    {

    }
}

GetByPostIdRequest如下:

public class GetByPostIdRequest 
{
    [FromRoute]
    public Int32 PostId { get; set; }
    public String LanguageCode { get; set; }
    public IncludeExpression Include { get; set; }
}

所有参数获取值的唯一方法是:

  1. 已启用FromQuery,所以我没有错误Unsupported Media Type

  2. 在Request类中具有FromRoute来绑定PostId。

没有其他方法吗?

我尝试了以下操作,这对我来说是合理的,但不起作用:

[ApiController]
public class PostController : Controller 
{

    [HttpGet("posts/{postId:int:min(1)}")]
    public async Task<IActionResult> GetByPostId([FromRoute, FromQuery]GetByPostIdRequest request) 
    {
    }

}

0 个答案:

没有答案