当我试图通过提琴手测试我的 HTTP 帖子时,我的 MVC4 Webapi 在第一个 If 条件检查 smsReq.Body 时给了我空指针引用错误。smsReq 以 NULL 的形式出现。我究竟做错了什么?
这是我的发布方法和提琴手请求。我正在使用 MVC4 Web Api
using System.Web.Http;
using System.Web;
using Twilio.TwiML.Mvc;
using Twilio.Mvc;
public class ValuesController : ApiController
{
[HttpPost]
public HttpResponseMessage Postsms([FromBody]SmsRequest smsReq)
{
if (String.IsNullOrEmpty(smsReq.Body.ToString()))
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest));
}
else
{
string smsTextType = smsReq.Body.ToString();
HttpResponseMessage response = this.Request.CreateResponse(HttpStatusCode.Created, "Hello World");
return response;
}
这是 Fiddler 中我的帖子的样子:
User-Agent: Fiddler
Content-Type: application/json; charset=utf-8
Accept: application/xml
SmsSid: xxxx
AccountSid: xxxxx
From: xxxx
To: xxxx
Host: localhost:9999
Content-Length: 5
Body: abc