![](/img/trans.png)
[英]Trying to call the Scheduler API Methods but fails to work coming from Azure Mobile Service Job. Works locally
[英]Trying to call from Postman fails but works from app
我正在嘗試從郵遞員調用C#api端點。 我正在從工作的代碼簡化代碼:
url: https://localhost:44302/api/MyControllerName/MyMethodName
Method: Post
Headers:
Content-Type: application/json
Accept: application/json
Body: {"ids": "1234asdfasdfasdf3e"}
和我的控制器代碼:
[HttpPost]
[Route("api/MyControllerName/MyMethodName")]
public async Task PostNewIds([FromBody] string ids)
{ // <-- breakpoint here is never hit
var json = JsonConvert.SerializeObject(ids);
Debug.WriteLine(json);
}
但我在VS中遇到此錯誤,無法達到打開的大括號處設置的斷點
{"Unexpected character encountered while parsing value: {. Path '', line 1, position 1."}
在PostNewIds
操作中,您正在將請求正文作為string
參數反序列化,因此您的ids == @"{""ids"":""1234asdfasdfasdf3e""}"
。 如果您需要從請求中獲取ids
值,則可以添加一個模型:
public class MyMethodNameModel
{
public string Ids { get; set; }
}
現在,您可以在操作中使用此模型:
[HttpPost, Route("api/MyControllerName/MyMethodName")]
public async Task PostNewIds([FromBody] MyMethodNameModel model)
{
Debug.WriteLine(model.Ids);
}
或者,如果您願意,可以使用@Hasan建議並反序列化“手動”:
[HttpPost, Route("api/MyControllerName/MyMethodName")]
public async Task PostNewIds([FromBody] string body)
{
var obj = JsonConvert.DeserializeObject(ids) as JObject;
var val = obj["ids"] as JValue;
Debug.WriteLine(val.Value);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.