Angularjs und ASP.NET Web-Api Ursache 500 Internal Server Error auf $http.post

Ich bin mit Angularjs und ASP.NET Web-Api my $http.get, $http.put und $http.löschen funktioniert gut, aber wenn ich Anrufe, $http.post bekomme ich 500 Internal Server Error, hier ist mein code:

Meinem Angularjs Controller-Funktion einfügen:

$scope.insert = function () {
    var data = '{ "Id": "1", "Name": "test", "Category": "r", "Price": "456.00" }';

    $http.post('api/products', data).success(function (data) {
        alert("it works");

    }).error(function () {
        console.log(Error);
        alert('Error reading JSON file. - ' + data);
    });   
 }

Mein Modell in C#:

public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }

Mein controller in C#:

    public class ProductsController : ApiController
    {
       public void PostProduct(Product product)
        {
            if (product == null)
            {
                throw new ArgumentNullException("product is NULL");
            }

            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd = new SqlCommand("INSERT INTO [AdventureWorks2012].[Production].[Product2] ( [Name], [ListPrice], [ProductLine]) VALUES ('" + product.Name + "', '" + product.Price + "', '" + product.Category + "')", con);

                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }

Code nie erreicht brake Punkte in C# - controller und wirft 500 Internal Server Error.

Bitte beraten.

  • Bitte geben Sie die route-Konfiguration.
Schreibe einen Kommentar