I am trying to post some data to an UmbracoApiController. My problem is I cant get it to work as a POST. It works as a Get though. Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Http;
//using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using umbraco.NodeFactory;
using Umbraco.Core.Models;
using Umbraco.Web.WebApi;
namespace XXX
{
public class PropertiesController : UmbracoApiController
{
[System.Web.Mvc.HttpPost]
public string GetModel1([FromBody]string test)
{
string x = test;
return x;
}
[System.Web.Mvc.HttpGet]
public string GetModel2(string test)
{
string x = test;
return x;
}
}
var jqxhr = $.ajax({
url: "/umbraco/api/Properties/GetModel1",
type: "GET",
contentType: "application/json",
dataType: "json",
data: {
test: 'test1'
},
error: function (err) {
console.log(err);
}
}).done(function (data) {
try {
console.log('------------ Get model example ---------------------');
console.log(data);
} catch (ex) {
console.log(ex);
}
});
UmbracoiApiController Is not accepting POSTs
I am trying to post some data to an UmbracoApiController. My problem is I cant get it to work as a POST. It works as a Get though. Here is the code:
I think the attribute is from the wrong namespace
It should be
Dave
As dave state correct your namespace to [System.Net.Http.HttpPost] is for api controllers
[System.Web.Mvc.HttpPost] is used in "mvc" controllers
But i think your problem is in the signature of your method, currently u have
and you are passing in a json object with a string property called test, and your method does not match that.
[FromBody] just tells the the controller to try to bind a complex model from the message body.
if i am not wrong u have 2 fixes, create a model in C# that matches your json object
or hax around it and make it post to whatevarurl?test=haxed
i would allways go with the model cause it just works :)
You are correct Troels - and its a shame I cannot mark two replies as answers. I have to stringify the model to get it in.
Since then, I have created a c# model that matches what I need, and it is working.
Shiat happens mate :)
glad u got it fixed, did you go for the model ? or the hax ?
is working on a reply...