【问题标题】:WebApi AngularJS "The request is invalid" ErrorWebApi AngularJS“请求无效”错误
【发布时间】:2014-05-22 21:31:45
【问题描述】:

我正在开发一个 ASP.NET WebApi / AngularJS 项目。现在我想显示一个表格。转到“/api/Auftraege”会显示 Json 数据。但是 '/api/Auftaege/index' 会带来这个错误:

{"Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Threading.Tasks.Task`1[System.Web.Http.IHttpActionResult] GetKDAuftraege(Int32)' in 'Vis3.Controllers.AuftraegeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."}

所以我尝试将 .edmx 图中的“id”属性更改为“nullable = true”,因为有些条目是零。那没用。
知道我能做什么吗?除了数据库之外,我还缺少其他东西吗?可能是 app.js 中的文件路径?

以防万一……

app.js:

function ListCtrl($scope, $http) {

    $scope.data = [];
    $http.get("/api/Auftraege/GetKDAuftraeges")
    .then(function (result) {

        // Success
        angular.copy(result.data, $scope.data);
    },
    function () {

        // Error
        alert("Error");
    });

}

WebApi-控制器:

// GET api/Auftraege
        [HttpGet]
        public IEnumerable<KDAuftraege> GetKDAuftraeges()
        {
            var result = db.KDAuftraeges.Take(50).ToList();

            return result;
        }

Index.cshtml 中的 ng-repeat(如果我运行 Index.cshtml,则会弹出来自名为 ListCtrl 的角度控制器的错误警报):

<tr ng-repeat="i in data">
    <td>{{i.AngebotsNummer}}</td>
    <td>{{i.VerkaeuferName}}</td>
    <td>{{i.Bezeichnung}}</td>
</tr>

【问题讨论】:

    标签: c# angularjs asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    如果这是一个 WebApi REST 服务,您不需要或必须包含方法的名称。您只引用 Controller 类,而没有像这样的单词控制器:

     $http.get("/api/Auftraege").then(function(){});
    

    【讨论】:

    • 非常感谢,成功了!但是我的app.js也有错误,我错过了将结果传递给函数:-)
    猜你喜欢
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 2018-10-14
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多