FromBody值GET空 [英] FromBody value get null

查看:469
本文介绍了FromBody值GET空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Asp.Net网络表单应用 -
这是我POST方法在我Apicontroller

 公共无效后([FromBody]字符串值)
{
}

我与小提琴手后处理。结果
我这样做了实验。结果
但事实并非如此。

有什么问题。结果
你能帮忙吗?

我试过了,我失败了。

 公共无效后(MyViewModel模型)
{
   串AA = model.Value;
}公共类MyViewModel
{
   公共字符串值{获得;组; }
}

在提琴手:

 请求正文:
值=哈坎


解决方案

在提琴手POST正文有效载荷应该是:

  = foo_bar这样的名称

而不是:

 值= foo_bar这样的名称

这只是那些关于模型在Web API中结合奇怪的事情之一。如果你想支持值= foo_bar这样的名称在POST体内的有效载荷,你可以总是写一个视图模型:

 公共类MyViewModel
{
    公共字符串值{获得;组; }
}

,然后让你的方法采取这种观点模型参数:

 公共无效后(MyViewModel模型)
{
    ......与model.Value在这里工作如常
}

This is Asp.Net Webform application
This is my POST method in my Apicontroller

public void Post([FromBody]string value)
{
}

I'm with fiddler post process.
I did so experiment.
But it did not.

What is the problem.
Can you help?

I've tried it, I've failed.

public void Post(MyViewModel model)
{
   string aa = model.Value;
}

public class MyViewModel
{
   public string Value { get; set; }
}

In Fiddler:

Request Body:   
Value=hakan

解决方案

The POST body payload in Fiddler should be:

=foo_bar

instead of:

value=foo_bar

That's just one of those strange things about the model binding in the Web API. If you want to support value=foo_bar in the POST body payload you could always write a view model:

public class MyViewModel
{
    public string Value { get; set; }
}

and then have your method take this view model as parameter:

public void Post(MyViewModel model)
{
    ... work with model.Value here as usual
}

这篇关于FromBody值GET空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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