Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Binding to string in a controller

$
0
0

I am trying to force the Controller to bind a simple type from the request body

So I post through jquery ajax 

var params = {source,"example"}$.ajax({
type: "POST",
url: wsurl,
contentType: "application/json",
data: JSON.stringify(params),
dataType: "json",
success: onSuccess
})


I can receive that on the controller when using [FromBody] and creating a simple Model

public class stringParm
{
public string source{ get; set; }
}

[HttpPost]
public string[] myAction([FromBody] stringParm s)

{

return new string[] s.source

}



but this does not work (ASP.NET Core 1.1) when using

[HttpPost]
public string[] myAction([FromBody] string source)

{

return new string[] source

}

In that case, source is always null.

Any suggestions on how to make this work?

Thx, Pieter


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>