I have a C# WebAPI action. I'm trying to post a string (but a json stringified string) to it using React.
For some reason I'm getting a 405 Method Not Allowed when the content type is aopplication/json. Urlencoded does get into PostTransaction, but body is always null. What do I need to change to be able to get json in PostTransaction?
WebConfig
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
WebApiConfig
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
C# WebApi
[HttpPost]
[ActionName("PostTransaction")]
public string PostTransaction([FromBody] string body, string task, int key, string filters, string options)
{
// body is null from react when content type is urlencoded, but correct from jquery post
string returnString = "no data found";
if (body != null) {
returnString = body.ToString();
}
return returnString;
}
React
var dataToSend = JSON.stringify({ param1: "value1", param2: "value2"});
fetch('http://localhosturl/PostTransaction',
{
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: dataToSend
})
.then(response => {
}
JQuery Post
var dataToSend = JSON.stringify({ param1: "value1", param2: "value2"});
$.post('http://localhosturl/PostTransaction', { '': dataToSend }).done(function (data) {
console.debug(data); // returned data from Web API
});
Thanks