Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Angularjs和ASP.NET Web Api导致$http.post上出现500个内部服务器错误_C#_Asp.net_Angularjs_Asp.net Web Api - Fatal编程技术网

C# Angularjs和ASP.NET Web Api导致$http.post上出现500个内部服务器错误

C# Angularjs和ASP.NET Web Api导致$http.post上出现500个内部服务器错误,c#,asp.net,angularjs,asp.net-web-api,C#,Asp.net,Angularjs,Asp.net Web Api,我正在使用Angularjs和ASP.NET Web Api,我的$http.get、$http.put和$http.delete工作正常,但当我调用$http.post时,我得到500个内部服务器错误,下面是我的代码: My Angularjs控制器插入函数: $scope.insert = function () { var data = '{ "Id": "1", "Name": "test", "Category": "r", "Price": "456.00" }';

我正在使用Angularjs和ASP.NET Web Api,我的$http.get、$http.put和$http.delete工作正常,但当我调用$http.post时,我得到500个内部服务器错误,下面是我的代码:

My Angularjs控制器插入函数:

$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);
    });   
 }
我的C#模型:

我的控制器在C#中:

代码从未到达C#控制器中的制动点,并抛出500个内部服务器错误


请告知。

如果您所说的是真的,并且代码执行从未到达
ProductsController
中的
PostProduct()
操作方法,则问题必须在Web API管道的早期(例如,任何全局消息处理程序、筛选器等)或客户端

通过使用Fiddler、Postman等将相同的请求和数据发送到您的Web API,可以很容易地从流程中删除客户端代码。我知道这是一个基本的建议,但由于您没有澄清您是否在问题中尝试了这一步,因此我不得不提到它


如果请求在这样发送时工作正常,那么您的客户端代码就有问题。否则,在将请求路由到控制器之前,您需要查看服务器上执行的代码。

这很可能是路由问题。第一步是确定路线设置是否正确。使用此工具调试路由:

e、 g.根据您的路由,您可能需要使用以下url api/产品/后期产品。我还将在该操作方法上方放置一个[HttpPost]属性


如果路由是正确的,那么您的模型和Json如何传递给您的操作可能存在一些问题,因此路由器无法匹配正确的操作方法。我建议您使用Fiddler检查Http请求和响应,并查看确切的请求正文。

更新:问题已解决!正如djikay所建议的那样,我在Chrome浏览器中添加了Postman扩展,并发现它会在ProductsController中查看POST方法,并且会混淆调用哪个方法

显然,控制器应该只有GET、POST、PUT和DELETE方法,但我还包括了另一个方法,由GetProduct和GetAllProducts调用,以避免冗余。但是,看起来添加到控制器中的任何其他方法(不是GET、POST、PUT或DELETE)都被视为POST

我想我一直在使用WebForms,需要更新我的MVC知识


在任何情况下,这两个答案都对我找到解决方案非常有帮助,我感谢djikay和Aidin。

请提供路由配置。这是我的put函数,正如您所见,它是相同的,只是我使用$http.put而不是$http.post,这就是困扰我的地方:$scope.update=function(){var data='{Id:“1”,Name:“test”,Category:“r”,Price:“456.00”};$http.put('api/products/',data.).success(函数(数据){alert(“it works”);})。error(函数(){alert('error reading JSON file.-'+$scope.products[0].Price);});})我注意到,当我发出“put”函数时,内容类型是text/plain,它可以正常工作,但当我调用“post”时,内容类型是application/json,即使我尝试这样更改:$http.defaults.headers.post[“Content Type”]=“text/plain;charset=utf-8”;为什么这两种方法的内容类型不同?如果必须,如何更改?我不确定将内容类型更改为text/plain是否可行,但是您可以这样做,我尝试了引用链接中的代码,但内容类型仍然显示为“application/json”。这非常奇怪,正如我之前提到的“put”和“put”的代码“post”完全相同,只是一个触发“$http.put”方法,另一个触发“$http.post”。因此,“put”工作正常,“post”一点也不。更奇怪的是,当我将内容类型设置为“text/plain”,然后用javascript alert()预览它的值时,它确实显示内容类型已更改为“text/plain”“,但当我在Google Chrome developer tools中调用$http.post时,在网络选项卡下,它显示post的内容类型为“application/json”。你知道吗?是的,如果你没有为你的方法指定
[http…]
属性,并且你的方法名没有遵循Web API命名约定,那么它被认为是
post
()。
public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }
    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);
            }

        }
    }