C# MVC AngularJS

本文关键字:AngularJS MVC | 更新日期: 2023-09-27 18:19:38

代码:

[ResponseType(typeof(List<MyClass>))] 
public async Task<IHttpActionResult> Get() 
{ 
    var userId = User.Identity.Name; 
    var obj = await this.FindAllAsync(userId); 
    if (obj == null) 
    { 
        return this.NotFound(); 
    } 
    return this.Ok(obj); 
}

它似乎没有在服务器端抛出任何错误,但在角度控制器的客户端,有人有什么想法吗?

我最初的代码是:ResponseType(typeof(bool)),现在因为更改它不起作用,所以不允许返回List对象,或者我需要在Angular js文件或我的C#控制器类中做一些额外的事情来让它起作用,例如Json fy it?!?!

请参阅http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/build-a-single-page-application-(spa)-使用aspnetwebapi和angularjs作为代码库。我所做的只是更改其中一个调用以返回一个列表,我不明白为什么会有任何不同。除了将代码升级到新语法之外,我没有对客户端控制器类进行任何重大修改。

$http.get("/api/crosswordHelper").then(
    function (response) {
        //$scope.historyItems = data.userHistory;
        $scope.title = "This app allows you to find all of the words in a dictionary which obey a regular expression.";
        $scope.working = false;
    },
    function (response) {
        $scope.title = "Oops... something went wrong '" + response.statusText + "'";
        $scope.working = false;
    });

应用程序挂起一段时间(如10秒),然后显示"Oops…"错误消息,状态文本为"Internal Server error"。我还可以告诉你,对FindAllSync的调用会像我预期的那样返回一行。我不明白为什么我需要Json’fy,正如下面评论中所建议的那样,而原始代码没有。

C# MVC AngularJS

很抱歉浪费大家的时间,因为我找到了解决自己问题的方法——我又获得了假人奖。事实上,服务器每次都会停止,以呈现为我打开灯泡的显示。

尽管我的结果集是一个对象列表,但列表中只有一项,但对象本身包含一个58000多行的内部列表!

因此,我决定只返回我需要的内容,即我在类的适当内部列表字段中添加了[JsonIgnore]标记,因为我并不真正需要将此内部列表带到客户端。天哪,想象一下,如果我的清单上有不止一项。无论如何,现在几乎是同时反应!