如何在 C# MVC 中接收 JSON 正文(无 ajax)
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();
}
url 是我在 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();
}
我有一个客户端正在向我的 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();
}
url 是我在 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();
}