簡體   English   中英

在郵遞員中獲取接受參數的請求,當前發送不支持的媒體類型錯誤

[英]get request to accepts params in postman, currently sends a unsupported media type error

我使用 body 請求構建了不同的 get 方法來選擇一個 id(使用原始 json)。 但是,當我了解到 get 方法不支持正文請求時,我想進行更改,以便我可以在 postman insted 中使用參數。 但是,當我現在執行此操作時,會得到 415 Unsupported Media Type。

這是方法:

    public async Task<IEnumerable<RentReason>> getRentReason(RentReason model)
    {  
        var parameters = new DynamicParameters();
        parameters.Add("@rentId", model.rentreasonId);
        var getAllRentReason = await _sqlconnection.QueryAsync<RentReason>($@"SELECT 
        CostItem.ID as rentreasonId,
        CostItem.CostItemTypeID as claimReason,
        RequestRentServiceReason.Name as rentReason, 
        RequestRentServiceCartype.Name as RentServiceCarTypeID, 
        DateFrom, DateTo, Price as totalPrice 
        FROM CostItem 
        INNER JOIN RequestRentServiceReason ON CostItem.RentReasonID = RequestRentServiceReason.ID
        INNER JOIN RequestRentServiceCartype ON CostItem.RentServiceCartypeID = RequestRentServiceCartype.ID
        WHERE CostItem.ID = @rentId", parameters);
        return getAllRentReason;
    }

控制器:

 [HttpGet]
        public async Task<IActionResult> getRentReason(RentReason model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            try
            {
                var list = await _request.getRentReason(model);
                return Ok(list);
            }
            catch (Exception ex)
            {

                return BadRequest(ex.Message);
            }
            return Ok();
        }

接口類:

Task<IEnumerable<RentReason>> getRentReason(RentReason model);

在我在正文中發送請求之前,例如:

{
    "rentreasonId": "40"
}

但我想在 params insted 中發送它,例如:

https://localhost/Request/getRentReason?rentreasonId=40

有誰知道如何實現這一目標?

您可以使用[FromQuery]屬性將方法參數綁定到查詢參數。

在你的情況下,它應該是這樣的:

public async Task<IActionResult> getRentReason([FromQuery] int rentreasonId)

這將使您能夠使用<your controller path>/rentReason?rentreasonId=40


但是,我建議為此使用路由參數:

[HttpGet("/your/api/route/rentreason/{rentreasonId}")]
public async Task<IActionResult> getRentReason([FromRoute] int rentreasonId)

這會讓你得到類似<authority>/your/api/route/rentreason/40

供參考: FromRouteAttribute

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM