我试图从正文中获取JSON,但我总是得到null。
这是我在MVC中的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Mvc;
using System.Web.Http;
using System.Net.Http;
using System.Net;
using System.IO;
namespace WebApplication5.Controllers
{
public class NimsoftController : Controller
{
static readonly Dictionary<Guid, Alert> alerts = new Dictionary<Guid, Alert>();
// GET: Nimsoft
//public ActionResult CreateAlarm()
[System.Web.Mvc.HttpPost]
public HttpResponseMessage CreateAlarm([FromBody] Alert val)
{
Models.CreateAlarm CreateAlarm = new Models.CreateAlarm();
System.IO.File.AppendAllText(@"D:\log.txt", string.Join(",", val.message));
string jsonAlarm = new JavaScriptSerializer().Serialize(new
{
message = "Test Message"//string.Join(",",alert.alerts),
});
var response = new HttpResponseMessage(HttpStatusCode.Created)
{
Content = new StringContent(jsonAlarm)
};
return response;
}
}
}
和我的要求
User-Agent: Fiddler
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-type: application/x-www-form-urlencoded
Host: localhost:52151
Content-Length: 27 '
答案 0 :(得分:0)
您应将mediaType
的{{1}}参数设置为StringContent
:
application/json