简体   繁体   English

WebApi内部服务器错误

[英]WebApi Internal Server Error

I have 2 projects, a Front-End (AngularJS) and a Back-End (C# Web-Api). 我有2个项目,一个前端(AngularJS)和一个后端(C#Web-Api)。 The thing is, when the front-end query the api (eg GET localhost/api/Especialistas?rol=XXXX) I get a 500 error. 问题是,当前端查询api(例如GET localhost / api / Especialistas?rol = XXXX)时,出现500错误。 Here is the code of the API: 这是API的代码:

public IHttpActionResult GetEspecialista(string rol)
{
    Especialista especialista = db.Especialistas.First( e=> e.Rol == rol);
    if (especialista == null)
    {
        return NotFound();
    }
    return Ok(especialista);
}

The API is working, since I reach the return Ok(especialista) . 自从到达return Ok(especialista)以来,API一直在工作。

The Front-end is using this Restangular to query the API 前端使用此Restangular查询API

Restangular.one("Especialistas?rol=XXXX").get()

The Network console shows a request 200 OK OPTION , but a 500 Internal Server Error GET . 网络控制台显示请求200 OK OPTION ,但显示500 Internal Server Error GET

I tried a message handler in the WebApiConfig.cs file to check if the GET request was reaching the Api, and is indeed reaching it, so I don't know what happened, since I didn't change any configuration file. 我在WebApiConfig.cs文件中尝试了一个消息处理程序,以检查GET请求是否已到达Api,并且确实已经到达了Api,所以我不知道发生了什么,因为我没有更改任何配置文件。

Any clue on how to fix this problem will be appreciated, thanks. 感谢您提供有关如何解决此问题的任何线索。

If your action is called successfully, but still receive a 500 error, I think the error is created by the serializing of especialista object when converted to a HTTP response. 如果成功调用了您的操作,但仍然收到500错误,我认为该错误是由especialista对象的序列化(转换为HTTP响应时)引起的。

Most probably, serialization fails because of some navigation properties which creat cycles in your object graph. 很有可能,由于某些导航属性会在对象图中创建循环,导致序列化失败。 It is recommended to return simple objects, not entity framework models. 建议返回简单的对象,而不是实体框架模型。

Try the following: 请尝试以下操作:

var retObj = new { Prop1 = especialista.Prop1, Prop2 = especialista.Prop2 };
return Ok(retObj);

If above code works, I suggest creating service models "mirror" objects that should be populated based on your data models. 如果上述代码可行,则建议创建应基于数据模型填充的服务模型“镜像”对象。 These objects should be returned instead of data models. 应该返回这些对象而不是数据模型。

A good helper to avoid the boilerplate code of property value assignments is Automapper . 好帮手避免财产赋值的样板代码是Automapper

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

相关问题 WebApi 500内部服务器错误 - WebApi 500 Internal Server Error 使用 WebAPI 2 和 CORS 时出现 500 内部服务器错误 - Getting 500 Internal Server Error while using WebAPI 2 and CORS WebAPI 核心使用异步控制器返回内部服务器错误 - WebAPI core is returning Internal Server Error with async controllers 内部服务器错误:部署WebAPI后,缓冲区不能为空 - Internal server error: Buffer cannot be null after deploying my WebAPI 从ajax调用WebApi时如何调试500内部服务器错误? - How can I debug a 500 Internal Server Error when calling a WebApi from ajax? Azure虚拟目录Webdeploy之后的Asp.Net Core WebApi内部服务器错误 - Asp.Net Core WebApi Internal Server Error after azure virtual directory webdeploy 在以下情况下出现500内部服务器错误:(冒号)位于WebApi Url的末尾。 接受ipv6作为参数 - 500 internal server error when : (colon) is at the end of WebApi Url. Accepting ipv6 as param 显示内部服务器错误? - Show internal server error? 具有基本身份验证的WebApi会给IIS提供500个内部错误 - WebApi with Basic Authentication gives 500-internal error with IIS IIS 7.5中的ASP.NET Core WebAPI 500内部错误 - ASP.NET Core WebAPI 500 Internal error in IIS 7.5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM