Search code examples
asp.netangularjswebformsasp.net-apicontroller

AngularJS on old WebForms


I need help working with Angular 1.5 on an existing old website (WebForms/Umbraco 4).

GET works

C#

public class MyController : ApiController
{
    [System.Web.Http.HttpGet]
    public string Test(int id)
    {
        return "ok";
    }
}

JS

$http.get("/api/My/Test?id=1");

but POST doesn't work

C#

public class MyController : ApiController
{
    [System.Web.Http.HttpPost]
    public string Test(int id)
    {
        return "ok";
    }
}

JS

$http.post("/api/My/Test", {id: 1});

No HTTP resource was found that matches the request URI 'http://localhost:8431/api/My/Test'.


How to make it work with POST?


Solution

  • This is what I ended up doing that solves the issue

    [System.Web.Http.HttpPost]
    public string Test()
    {
        string jsonStr = Request.Content.ReadAsStringAsync().Result;
        dynamic data = JObject.Parse(jsonStr);
        var id = data.id.Value;
        return "ok";
    }