[英]How to receive a JSON body in C# MVC (no ajax)
我有一個客戶端正在向我的asp.net mvc應用程序發送一個json。 我在哪里可以收到json正文?
發送:
var client = new RestClient(uri);
client.Authenticator = new NtlmAuthenticator();
RestRequest requestCom =
new RestRequest("", method);
//add headers
requestCom.AddHeader("Accept", "application/json");
if (body != null)
{
requestCom.AddJsonBody(body);
}
IRestResponse response = client.Execute(requestCom);
控制器:
public string Index([FromBody]object body)
{
return body.ToString();
}
網址是我在mvc應用程序中的控制器。 那我怎樣才能收到屍體呢?
假設您有一個這樣的模型
public class MyModel {
public string AProperty { get; set; }
}
並發送到服務器
var client = new RestClient(uri);
client.Authenticator = new NtlmAuthenticator();
var requestCom = new RestRequest("", method);
//add headers
requestCom.AddHeader("Accept", "application/json");
var body = new MyModel {
AProperty = "Hello World!!!"
}
if (body != null) {
requestCom.AddJsonBody(body);
}
IRestResponse response = client.Execute(requestCom);
Controller動作必須期望體內有模型
public string Index([FromBody]MyModel body) {
return body.ToString();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.