为什么我只在将数据发布到web api时才收到500错误? [英] Why I am getting the 500 error only when I am posting the data to web api?

查看:148
本文介绍了为什么我只在将数据发布到web api时才收到500错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试将数据发布到web api我收到500错误并且没有调用方法。代码如下



这是Model类





< pre lang =c#> public class 类别
{
public int CategoryId { get ; set ; }
public string CategoryName { get ; set ; }
public string ParentCategoryId { get ; set ; }
public string IsServiceContainer { get ; set ; }
public string ImageUrl { get ; set ; }
public string 描述{ get ; set ; }
public 列表<类别> ChildCategories { get ; set ; }
public string IsActive { get ; set ; }
}







这是控制器方法



 [ResponseType( typeof (Category))] 
public IHttpActionResult PostCategory(类别类别)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
ObDataAccess.InsertCategory(category.CategoryName,category.ImageUrl,category.ParentCategoryId,category.IsActive,category.Description,category.IsServiceContainer);
return CreatedAtRoute( DefaultApi new {id = category.CategoryId},category);
}







这是Angular js服务



  //   /<参考path =D:\FetotAdmin \FetotAdmin \FetotAdmin \Scripts / angular.js/>  
var CategoryService = angular.module(' CategoryService',[]);
CategoryService.factory(' CategoryApi'函数($ http){
var urlBase = http:// localhost:60624 / api;
var CategoryApi = {};
CategoryApi .getCategorees = function (){
return $ http.get(urlBase + ' / Category');

};
CategoryApi.AddCategory = < span class =code-keyword> function (category){
alert( JSON .stringify(category));
// var req = $ http({
// 方法: 'post',
// url:urlBase +'/ Category',
// data:category

// });
// < span class =code-comment> return req;

return $ http.post(' http:// localhost:60624 / api / Category /',category);
};
return CategoryApi;

});





我的尝试:



我尝试使用$ http.post方法。当我调用使用Entity框架创建的web api时,它工作正常,但是当我使用post操作调用self self web api时,仅在发布数据的情况下给出500错误

解决方案

http){
var urlBase = HTTP://本地主机:60624 / API;
var CategoryApi = {};
CategoryApi.getCategorees = function (){
return


http.get(urlBase + ' / Category');

};
CategoryApi.AddCategory = function (类别){
alert( JSON .stringify (类别));
// var req =


http({
// 方法:'post',
// url:urlBase +'/ Category',
// 数据:类别

// });
// return req;

return


Hi I am trying to post data to web api I am getting 500 error and method is not being called . The code is below

This is the Model class


public class Category
  {
      public int CategoryId { get; set; }
      public string CategoryName { get; set; }
      public string ParentCategoryId { get; set; }
      public string IsServiceContainer { get; set; }
      public string ImageUrl { get; set; }
      public string Description { get; set; }
      public List<Category> ChildCategories { get; set; }
      public string IsActive { get; set; }
  }




This is the controller Method

[ResponseType(typeof(Category))]
     public IHttpActionResult PostCategory(Category category)
     {
         if (!ModelState.IsValid)
         {
             return BadRequest(ModelState);
         }
         ObDataAccess.InsertCategory(category.CategoryName, category.ImageUrl, category.ParentCategoryId, category.IsActive, category.Description, category.IsServiceContainer);
         return CreatedAtRoute("DefaultApi", new { id = category.CategoryId }, category);
     }




this is the Angular js Service

/// <reference path="D:\FetotAdmin\FetotAdmin\FetotAdmin\Scripts/angular.js" />
var CategoryService = angular.module('CategoryService', []);
CategoryService.factory('CategoryApi', function ($http) {
    var urlBase = "http://localhost:60624/api";
    var CategoryApi = {};
    CategoryApi.getCategorees = function () {
        return $http.get(urlBase + '/Category');

    };
    CategoryApi.AddCategory = function (category) {
        alert(JSON.stringify(category));
        //var req = $http({
        //    method: 'post',
        //    url: urlBase + '/Category',
        //    data: category

        //});
        //return req;

        return $http.post('http://localhost:60624/api/Category/', category);
    };
    return CategoryApi;

});



What I have tried:

I tried with $http.post method. when I am calling the web api created with Entity framework it is working fine but when I am calling self created web api with post operation its giving 500 error only in case of post data

解决方案

http) { var urlBase = "http://localhost:60624/api"; var CategoryApi = {}; CategoryApi.getCategorees = function () { return


http.get(urlBase + '/Category'); }; CategoryApi.AddCategory = function (category) { alert(JSON.stringify(category)); //var req =


http({ // method: 'post', // url: urlBase + '/Category', // data: category //}); //return req; return


这篇关于为什么我只在将数据发布到web api时才收到500错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆