0

i have angular.js code like this :

(function (appAN) {
    'use strict';
    appAN.controller('InfoController', function ($scope, $http) {
        $scope.model = {
            firstname: "john",
            lastname: "doe",
            company: 'IBM',
        }
        $scope.addbtn = function () {
            var info = {
                firstname: $scope.model.firstname,
                lastname: $scope.model.lastname,
                company: $scope.model.company
            }
            var url = "Info/AddInfo";
            var data = JSON.stringify(info);
            $http.post(url, data).success(function (data) {
                alert(success);
            });
        }
    });
}(angular.module('testd',[])));

and in c# code like this :

namespace testdemo.Controllers
{
    public class InfoController : ApiController
    {
        [HttpPost]
        public IHttpActionResult AddInfo([FromBody]InfoClass info)
        {
            object o = new object();
            return Json(o);
        }
    }

    public class InfoClass
    {
        public string firstname { get; set; }
        public string lastname { get; set; }
        public string company { get; set; }
    }
}

now problem is : i am not able to get values in my c# method, breaking point is not hitting also at 'AddInfo' method.

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:63498/Info/AddInfo

i am able to see the values untill : var data = JSON.stringify(info);

what is wrong with my code ?

my routing is like this :

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
             name: "DefaultApi",
             routeTemplate: "api/{controller}/{id}",
             defaults: new { id = RouteParameter.Optional }
         );
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
    }
}
6
  • 1.Could you please tell what you are doing in the post method? 2. Did you check the api whether it's working or not in the fiddler or any rest client? 3. Try adding the url as "api/Info/AddInfo" Commented Aug 5, 2015 at 11:14
  • yes, i got error as not found 404 !, did i miss routing information for my ApiController? Commented Aug 5, 2015 at 11:22
  • You should look into webapiconfig.cs Commented Aug 5, 2015 at 11:27
  • call api as follows in the fillder. localhost:63498/api/Info/AddInfo Commented Aug 5, 2015 at 11:28
  • same thing, not able to fix. Commented Aug 5, 2015 at 11:55

3 Answers 3

1

You should register your routes for ApiControllers in App_Start/WebApiConfig.cs. Something like this:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

    }
}

And then use url like this:

var url = "/api/Info/AddInfo"; 
Sign up to request clarification or add additional context in comments.

Comments

0

remove JSON.stringify from your code and directly pass info object in your $http.post. Angular will stringify it for you :)

You need to annotate your post handler for a rest end.

Add you post URL in RouteConfig

Comments

0

Make sure you have the following snippet in the webapiconfig.cs to call webapi.

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

Mention api url as follows in the "api/Info/AddInfo"

2 Comments

OK, i did exactly same ! and error i got it is : 'POST localhost:63498/api/Info/AddInfo 404 (Not Found)'
did you do the changes in the webapiconfig.cs file.looks like you are using the MVC stuff over there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.