簡體   English   中英

Webapi POST在MVC Controller中產生{object}

[英]Webapi POST results in {object} in MVC Controller

我想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;
        })

如果要在POST正文中發送object ,則還應該在ASP.NET Endpoint方法中檢索object 將您的string str包裝到一個class

public class MyRequest
{
    public string Str { get; set; }
}

[HttpPost]
public void Foo(MyRequest request)
{
    ...
}

將內容類型更改為text/plain然后使用JSON.stringify將對象轉換為字符串

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM