我正在尝试在 C# 中获取 POST 数据,我读过的所有内容都说要使用
Request.Form["parameterNameHere"]
我正在尝试这样做,但我收到一条错误消息
System.Net.Http.HttpRequestMessage 不包含 Form 的定义,也不包含 Form 的扩展方法。
有问题的方法是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.HttpRequest;
namespace TextServer.Controllers
{
public class TextController : ApiController
{
// POST api/<controller>
public HttpResponseMessage Post([FromBody]string value)
{
string val = Request.Form["test"];
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StringContent("Your message to me was: " + value);
return response;
}
任何帮助是极大的赞赏。
您应该在请求正文中传递对象并从正文中检索值:
public HttpResponseMessage Post([FromBody] SomeModel model)
{
var value = model.SomeValue;
...
或者,如果您只需要字符串:
public HttpResponseMessage Post([FromBody] string value)
{
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StringContent("Your message to me was: " + value);
return response;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)