如何接收和显示发送到ASP.NET的POST参数

时间:2016-09-09 18:07:49

标签: c# asp.net-mvc post

我正在尝试从 ASP.NET MVC 解决方案中的 Mirth Connect 获得HTTP POST,但我不知道这些数据是如何传达给我的Web应用程序。我创建了一个控制器来接收它:

public class IntegrationController : Controller
{

    public ActionResult Index()
    {
        var c = HttpContext.CurrentHandler;
        var v = c.ToString();
        Console.Write("The value is" + v);

        return View();
    }
}

我应该在Index()收到什么?一本字典?一旦我收到它,如何在观众中如何?

谢谢。

2 个答案:

答案 0 :(得分:1)

我使用[FromBody] Annotation,然后我可以像任何其他参数一样对待传入。

    [Route("api/gateways")]
    [Route("api/connectedDevices")]
    [Route("api/v0/gateways")]
    [Route("api/v0/connectedDevices")]
    [HttpPost]
    public HttpResponseMessage Create([FromBody] IGatewayNewOrUpdate device,

我确实有一个案例,当我直接从请求中读取内容;

    [HttpPost]
    public HttpResponseMessage AddImageToScene(long id, string type)
    {
        SceneHandler handler = null;

        try
        {

            var content = Request.Content;

我认为不同版本可能会有所不同,但我希望这会给你一个起点。

答案 1 :(得分:1)

尝试这样的事情。如果v是将从表单发布的字段的名称,这将有效。

public class DataModel
{
    public string v {get; set;}
}

public class IntegrationController : Controller
{
    [HttpPost]
    public ActionResult Index(DataModel model)
    {
        Console.Write("The value is" + model.v);

        return View();
    }
}

如果您收到Json,则可以添加[FromBody]

public class IntegrationController : Controller
{
    [HttpPost]
    public ActionResult Index([FromBody] DataModel model)
    {
        Console.Write("The value is" + model.v);

        return View();
    }
}