简体   繁体   English

WebApi AngularJS“请求无效”错误

[英]WebApi AngularJS “The request is invalid” Error

I'm working on a ASP.NET WebApi / AngularJS project. 我正在研究ASP.NET WebApi / AngularJS项目。 For now I want to display a table. 现在,我想显示一个表。 Going to '/api/Auftraege' shows the Json Data. 转到'/ api / Auftraege'将显示Json数据。 But '/api/Auftaege/index' brings this error: 但是'/ 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."}

So I tried to change the property of the 'id' in the .edmx Diagram to 'nullable = true', because there are entries with zeros. 因此,我尝试将.edmx图中'id'的属性更改为'nullable = true',因为其中存在带有零的条目。 That didn`t work. 那没用。
Any idea what I can do? 知道我能做什么吗? Am I missing something else besides the database? 除了数据库,我还缺少其他东西吗? Perhaps the file path in the app.js? 也许是app.js中的文件路径?

Just in case... 以防万一...

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-Controller: 的WebAPI - 控制器:

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

            return result;
        }

The ng-repeat within the Index.cshtml(If I run the Index.cshtml, then the error alert from the angular controller named ListCtrl pops up): 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>

If this is a WebApi REST Service you do not need or have to include the name of the method. 如果这是WebApi REST服务,则不需要或不必包含方法的名称。 You only refer to the Controller class without the word controller like this: 您只能引用Controller类,而不能这样使用单词controller:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM