c# - IHttpActionResult in MVC not firing from angular.js $http.post method -


i have angular.js code :

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

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 : not able values in c# method, breaking point not hitting @ 'addinfo' method.

failed load resource: server responded status of 404 (not found) http://localhost:63498/info/addinfo

i able see values untill : var data = json.stringify(info);

what wrong code ?

my routing :

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"));     } } 

you should register routes apicontrollers in app_start/webapiconfig.cs. 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 use url this:

var url = "/api/info/addinfo";  

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -