2
バックエンド、ASP.netコアAPI:Angular 2からASP.net CoreへのPOST要求が機能しません。サーバー側でヌル値
[Produces("application/json")]
[Route("api/[controller]")]
public class StoriesController : Controller
{
public static List<Story> STORIES = new List<Story>
{
new Story
{
content = "Some really interesting story about dog",
timeOfAdding = new DateTime(2016, 8, 26),
numberOfViews = 11
},
new Story
{
content = "Even cooler story about clown",
timeOfAdding = new DateTime(2016, 9, 26),
numberOfViews = 11
},
new Story
{
content = "And some not cool story",
timeOfAdding = new DateTime(2016, 10, 26),
numberOfViews = 11
}
};
// POST api/values
[HttpPost]
public void Post([FromBody]string value)
{
Story story = new Story
{
content = value,
timeOfAdding = DateTime.Now,
numberOfViews = 0
};
STORIES.Add(story);
}
}
活字体の機能:(Firefoxのコンソールで見られる)が送信
add(content: string): Observable<Story> {
let body = JSON.stringify({ "value": content });
//let body = { "value": content };
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers});
return this.http.post(this.heroesUrl, body, options)
.map(this.extractData)
.catch(this.handleError);
}
パラメータ:
Visual Studio 2015デバッガのvalue = null
どういうところが間違っていますか?私はインターネットで見つけたすべてを試しました。つまり、ヘッダの追加/削除、JSON.stringifyの削除、[FromBody]
属性の追加/削除です。結果は毎回同じです。
1つの文字列パラメータを渡したい場合は、別のクラスを作成する必要がありますか? AFAIK文字列はクラスそのものなので、なぜ文字列を渡すことができないのですか? – Piotrek
文字列だけでなく、そこにJSONを渡しています。 – rinukkusu
したがって、内部JSONデシリアライザは '{" value ":" yyyyy "}'を 'String'クラスに逆シリアル化できません。 – rinukkusu