Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jagdip Ajimal 2 posts 82 karma points
    Sep 16, 2015 @ 10:25
    Jagdip Ajimal
    0

    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:

        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);
                    }
                });
    
  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Sep 16, 2015 @ 15:36
    Dave Woestenborghs
    100

    I think the attribute is from the wrong namespace

    It should be

    [System.Net.Http.HttpPost]
    

    Dave

  • Troels Larsen 75 posts 280 karma points
    Sep 16, 2015 @ 16:44
    Troels Larsen
    1

    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

    public string GetModel1([FromBody]string test)
    

    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 :)

  • Jagdip Ajimal 2 posts 82 karma points
    Sep 21, 2015 @ 12:11
    Jagdip Ajimal
    0

    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.

  • Troels Larsen 75 posts 280 karma points
    Sep 21, 2015 @ 13:46
    Troels Larsen
    0

    Shiat happens mate :)

    glad u got it fixed, did you go for the model ? or the hax ?

Please Sign in or register to post replies

Write your reply to:

Draft