gpt4 book ai didi

c# - POST 抛出 HttpRequestMessage 不包含 Form 的定义

转载 作者:行者123 更新时间:2023-11-30 12:28:04 24 4
gpt4 key购买 nike

我正在尝试在 C# 中获取 POST 数据,我读过的所有内容都说要使用

Request.Form["parameterNameHere"]

我正在尝试,但我收到一条错误消息

System.Net.Http.HttpRequestMessage does not contain a definition for Form and no extension method for 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;
}

关于c# - POST 抛出 HttpRequestMessage 不包含 Form 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23277058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com