如何从操作方法中 POST Json 没有 jquery 的数据并读取响应
How to POST Json data without jquery from an action method and read the response
澄清:POST请求在同一项目的不同网站或不同控制器之间。
我想POSTJson数据到另一个action方法。我正在使用 Newtonsoft 进行 json 序列化,问题是在 Json 方法中,字段名称带有空值。我错过了什么吗?
我的模特:
class Person
{
[JsonProperty("name")]
public string Name{get;set;}
}
postjson的动作方法:
public ActionResult Method1()
{
Person p = new Person(){Name = "Test"}
string urlToRedirect = "..urlRoute../JsonMethod";
var res = SendRequest(urlToRedirect, p);
//...do anything with res
}
接收模型的方法
[HttpPost]
public ActionResult JsonMethod(Person p)
{
if(p.Name == "Test")
return Json("ok");
else return Json("bad");
}
发送请求的方法
public async bool SendRequestAsync(string requestUrl, object data)
{
string json = JsonConvert.SerializeObject(obj, Formatting.Indented,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
if (request != null)
{
request.Accept = "application/json";
request.ContentType = "application/json";
request.Method = "POST";
using (var stream = new StreamWriter(await request.GetRequestStreamAsync()))
{
stream.Write(json);
}
using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse)
{
if (response != null && response.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
response.StatusCode,
response.StatusDescription));
if (response != null)
{
Stream responseStream = response.GetResponseStream();
//return true or false depending on the ok
return GetResponseModel(responseStream);
}
}
}
}
catch (WebException ex)
{
var response = ex.Response;
Stream respStream = response.GetResponseStream();
//return true or false depending on the ok
return GetResponseModel(respStream);
}
catch (Exception e)
{
return false;
}
return false;
}
我的错误是人身上的"name"属性属性,必须是大写字母"Name"。这就是为什么它带有 null。
[JsonProperty("Name")]
澄清:POST请求在同一项目的不同网站或不同控制器之间。
我想POSTJson数据到另一个action方法。我正在使用 Newtonsoft 进行 json 序列化,问题是在 Json 方法中,字段名称带有空值。我错过了什么吗?
我的模特:
class Person
{
[JsonProperty("name")]
public string Name{get;set;}
}
postjson的动作方法:
public ActionResult Method1()
{
Person p = new Person(){Name = "Test"}
string urlToRedirect = "..urlRoute../JsonMethod";
var res = SendRequest(urlToRedirect, p);
//...do anything with res
}
接收模型的方法
[HttpPost]
public ActionResult JsonMethod(Person p)
{
if(p.Name == "Test")
return Json("ok");
else return Json("bad");
}
发送请求的方法
public async bool SendRequestAsync(string requestUrl, object data)
{
string json = JsonConvert.SerializeObject(obj, Formatting.Indented,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
if (request != null)
{
request.Accept = "application/json";
request.ContentType = "application/json";
request.Method = "POST";
using (var stream = new StreamWriter(await request.GetRequestStreamAsync()))
{
stream.Write(json);
}
using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse)
{
if (response != null && response.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
response.StatusCode,
response.StatusDescription));
if (response != null)
{
Stream responseStream = response.GetResponseStream();
//return true or false depending on the ok
return GetResponseModel(responseStream);
}
}
}
}
catch (WebException ex)
{
var response = ex.Response;
Stream respStream = response.GetResponseStream();
//return true or false depending on the ok
return GetResponseModel(respStream);
}
catch (Exception e)
{
return false;
}
return false;
}
我的错误是人身上的"name"属性属性,必须是大写字母"Name"。这就是为什么它带有 null。
[JsonProperty("Name")]