0
不完全確定這是否是原因,但這是我目前唯一的理論。目前,我的WebAPI嘗試發送任何通過控制器發出的GET
調用的響應。 CORS在主WebAPIConfig中啓用,但任何時候響應包含另一個類,我得到一個InternalServerError 500
。 500失敗如果規則有一個元素/視圖包含在發送的數據中 - 否則,通過罰款。如果響應是複雜的,Api會發送Error500對象
// 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);
}
}
這可能是客戶端的錯誤嗎?
// 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;
}
}
500表示服務器端發生意外故障,對嗎?通常找到原因的方法是檢查服務器端日誌,例如,在'%SystemDrive%\ inetpub \ logs \ LogFiles'和/或'%SystemDrive%\ Windows \ System32 \ LogFiles \ HTTPERR'中。另見http://stackoverflow.com/questions/6426375/where-can-i-find-the-iis-logs/6426399#6426399 – sideshowbarker
IRepository不應該是Mock> _repo; –
正如@sideshowbarker所說,絕對不是客戶端問題。如果你調試,錯誤在哪裏拋出?如果您調試,什麼是實際的異常消息? – MichaelDotKnox