1

我在OData APICustomEntityController的模型控制器中制作了这个功能代码CustomEntity

public HttpResponseMessage Post([FromBody] CustomEntity entity)
{
    HttpResponseMessage response = null;


    SecurityController sc = new SecurityController();
    // if (sc.CheckPermission(User.Identity.Name, "CustomEntity", "Write")) {
    if (sc.CheckPermission("User", "CustomEntity", "Write"))
    {
        _context.CustomEntity.Add(entity);
        _context.SaveChanges();
        StringContent stringContent = new StringContent(entity.ToString());
        response = new HttpResponseMessage(HttpStatusCode.Created)
        {

            Content = stringContent
        };
        response.Headers.Add("Location", string.Format("http://{0}:{1}/CustomEntity", HttpContext.Request.Host.Host, HttpContext.Request.Host.Port));
    }

    return response;
}

当客户端想要创建一个CustomEntity时,它会调用该Post函数。在 API 服务器端一切正常,实体被添加到关联的数据库中_context。但是当我从客户端使用 Post 时,客户端会抛出一个错误

500内部服务器错误)。

反应不好形成?

这是客户端代码:

Container cont;
cont = new Container(new Uri("http://localhost:51342/")); 
cont.Credentials = credentials;


CustomEntity lr = new CustomEntity();
lr.AreaPath = "AreaPath";
lr.Description = "Description";
lr.Frecuency = "Frecuency";
lr.Name = "Name";
lr.Reason ="Reason";
lr.RequestArea = "RequestArea";
cont.AddToCustomEntity(lr);
cont.SaveChanges(); //throws an InternalServerError
4

0 回答 0