Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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
Javascript 如果响应是复杂对象,Api将发送Error500_Javascript_.net_Angular_Asp.net Web Api - Fatal编程技术网

Javascript 如果响应是复杂对象,Api将发送Error500

Javascript 如果响应是复杂对象,Api将发送Error500,javascript,.net,angular,asp.net-web-api,Javascript,.net,Angular,Asp.net Web Api,不完全确定这是否是原因,但这是我目前唯一的理论。目前,我的WebAPI尝试向控制器发送任何GET调用的响应。CORS在主WebAPIConfig中被启用,但是每当响应包含另一个类时,我都会得到一个InternalServerError 500。如果规则在发送的数据中包含一个元素/视图,则该规则将失败,返回500,否则将通过罚款 // Server Side public class RuleController : ApiController { private readonly IRe

不完全确定这是否是原因,但这是我目前唯一的理论。目前,我的WebAPI尝试向控制器发送任何
GET
调用的响应。CORS在主WebAPIConfig中被启用,但是每当响应包含另一个类时,我都会得到一个
InternalServerError 500
。如果规则在发送的数据中包含一个元素/视图,则该规则将失败,返回500,否则将通过罚款

// Server Side
public class RuleController : ApiController
{
    private readonly IRepository<Rule> _repo;

    public RuleController()
    {
        var mock = new Mock<IRepository<Rule>>();
        mock.Setup(m => m.GetAll()).Returns(new List<Rule>
        {
            new Rule
            {
                Id = 2,
                Name = "TestRule819416",

                // If either of these elements are uncommmented, a 500 error is returned, otherwise it comes back fine.
                //Element = new Element {
                //    Id = 9461313,
                //    Name = "safjwofmfs"
                //},
                //View = new View
                //{
                //    Id = 3269591,
                //    Name = "sofijhopfjsapof"
                //}
            },

        }.AsQueryable());

        _repo = mock.Object;
    }

    [ResponseType(typeof(Rule))]
    public IHttpActionResult GetAll()
    {
        var elements = _repo.GetAll();

        if (elements == null)
            return NotFound();

        return Ok(elements);
    }
}
//服务器端
公共类规则控制器:ApiController
{
私人只读电子回购;
公共规则控制器()
{
var mock=new mock();
mock.Setup(m=>m.GetAll()).Returns(新列表
{
新规则
{
Id=2,
Name=“TestRule819416”,
//如果这些元素中的任何一个未注释,则返回500错误,否则返回正常。
//元素=新元素{
//Id=9461313,
//Name=“safjwofmfs”
//},
//视图=新视图
//{
//Id=3269591,
//Name=“sofijhopfjsapof”
//}
},
}.AsQueryable());
_repo=mock.Object;
}
[响应类型(类型(规则))]
公共IHttpActionResult GetAll()
{
var elements=_repo.GetAll();
if(elements==null)
返回NotFound();
返回Ok(元素);
}
}
这可能是客户端的错误吗

// Client Side
// 
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';

import 'rxjs/add/operator/toPromise';

import { Rule } from '../domain/Rule';

@Injectable()
export class ErrorService {
    constructor(private http: Http) {

    }

    private rulesUrl = 'api/rule';

    getRules(): Promise<Rule[]> {
        return this.http.get(this.rulesUrl)
            .toPromise()
            .then(this.extractData)
            .catch(this.handleError);
    }

    private handleError(error: any): Promise<any> {
        console.error('An error occured attempt to retrieve from server', error);

        return Promise.reject(error.message || error);
    }

    private extractData(response: Response): Rule[] {
        let data = response.json() as Rule[];
        return data;
    }
}
//客户端
// 
从“@angular/core”导入{Injectable};
从'@angular/Http'导入{Headers,Http,Response};
导入“rxjs/add/operator/toPromise”;
从“../domain/Rule”导入{Rule};
@可注射()
导出类错误服务{
构造函数(专用http:http){
}
private rulesUrl='api/rule';
getRules():承诺{
返回this.http.get(this.rulesUrl)
.toPromise()
.然后(此.extractData)
.接住(这个.把手错误);
}
私有句柄错误(错误:任意):承诺{
console.error('尝试从服务器检索时出错',错误);
返回承诺。拒绝(error.message | | error);
}
私有提取数据(响应:响应):规则[]{
让data=response.json()作为规则[];
返回数据;
}
}

正如@MichaelDotKnox和@sideshowbarker所提到的,我需要检查内部日志,以找出服务器没有发送信息的原因

要总结他们的答案,请检查以下位置的日志,了解IIS正在做什么:

%SystemDrive%\inetpub\logs\LogFiles
和/或

%SystemDrive%\Windows\System32\LogFiles\HTTPERR
然后逐步检查代码,查看异常可能发生的位置


在我的例子中,我的类正遭受着一种
InvalidCastException
的痛苦,它被所有的混乱所吞噬。一旦我找到并解决了它,我的甜蜜数据就开始毫无故障地发送过来。

500表示服务器端出现意外故障,对吗?通常查找原因的方法是检查服务器端日志,例如,在
%SystemDrive%\inetpub\logs\LogFiles
和/或
%SystemDrive%\Windows\System32\LogFiles\HTTPERR
中。请看,IRepository回购不应该是模拟回购吗;正如@sideshowbarker所说,这绝对不是客户端的问题。如果调试,错误会抛出到哪里?如果您调试,实际的异常消息是什么?@MarcusH我不认为这是问题所在,这只是常规的DIb+模拟。@sideshowbarker&MichaelDotKnox是的,我很确定它也是服务器端的,但只是想可能与请求有关?在返回OK响应后,.NET代码中的某个地方会抛出错误-因此我无法很好地调试它。同时,我会按照你建议的sideshowbarker检查这些日志-谢谢!