POST抛出HttpRequestMessage不包含Form的定义 [英] POST throws HttpRequestMessage does not contain a definition for Form

查看:401
本文介绍了POST抛出HttpRequestMessage不包含Form的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get POST data in C# and everything I have read says to use

Request.Form["parameterNameHere"]

我正在尝试尝试此操作,但出现错误提示

I am trying that, but I get an error saying


System.Net.Http.HttpRequestMessage不包含Form的定义,也不包含Form的扩展方法。'

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;
    }

任何帮助将不胜感激。

推荐答案

您应该在请求正文中传递您的对象,并从正文中检索值:

You should pass your object in the request body and retrieve values from the body:

public HttpResponseMessage Post([FromBody] SomeModel model)
{
    var value = model.SomeValue;
    ...

或者如果您需要的只是字符串:

Or if all you need is the string:

public HttpResponseMessage Post([FromBody] string value)
{
    HttpResponseMessage response = new HttpResponseMessage();
    response.Content = new StringContent("Your message to me was: " + value);
    return response;
}

这篇关于POST抛出HttpRequestMessage不包含Form的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆