Webapi POST在MVC Controller中生成{object}

时间:2018-06-14 12:04:49

标签: c# angularjs asp.net-web-api

我尝试post stringMVC Controller,结果始终为{object},我无法解析\解码\反序列化。我怎样才能得到那个字符串?

C#:

[HttpPost]
public void Foo(dynamic str)
{
    //str always equals to '{object}'
    var path = @"C:\cookieParserXmlOutput\";
}

客户代码:

then((res:any) => {
            let strObj={
                str:res.data
            };
            return this.$http.post("/App/Foo",strObj,{
                headers: { "Content-Type": "application/json; charset=utf-8" }
              });

        }).then((res:any)=>{
            return res;
        })

2 个答案:

答案 0 :(得分:2)

如果要在POST正文中发送object,还应该在ASP.NET端点方法中检索string str。将您的class换入public class MyRequest { public string Str { get; set; } } [HttpPost] public void Foo(MyRequest request) { ... }

var number = 4
var percentage = 100 / number
var somaPercentual = new Array();
for (i = 0; i < number; i++) {
  var percentage = percentage
  somaPercentual.push(percentage)
}

console.log(somaPercentual)

答案 1 :(得分:0)

将内容类型更改为text/plain,并使用JSON.stringify将对象转换为字符串

return this.$http.post("/App/Foo",JSON.stringify(strObj),{
      headers: { "Content-Type": "text/plain; charset=utf-8" }
});