Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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
C# 使用Web Api Http Put时出现内部服务器错误500_C#_Asp.net Web Api_Asp.net Web Api Routing - Fatal编程技术网

C# 使用Web Api Http Put时出现内部服务器错误500

C# 使用Web Api Http Put时出现内部服务器错误500,c#,asp.net-web-api,asp.net-web-api-routing,C#,Asp.net Web Api,Asp.net Web Api Routing,我正在为一家使用拣选流程的公司开发一个网络应用程序 这个过程非常简单,我有一个包含一些信息的销售订单,我创建了一个包含这些信息的拣货单(例如:商品数量、总重量和销售订单id) 我总是犯这样的错误 内部服务器错误 …当我尝试使用PUT方法时,创建一个领料单并将其添加到Web Api端的数据库中 当我删除[HttpPost]时,错误更改为: 找不到方法 或 方法不允许 这是我的代码: 客户端 public async Task<bool> AddTarefa(ListasPicking

我正在为一家使用拣选流程的公司开发一个网络应用程序

这个过程非常简单,我有一个包含一些信息的销售订单,我创建了一个包含这些信息的拣货单(例如:商品数量、总重量和销售订单id)

我总是犯这样的错误

内部服务器错误

…当我尝试使用PUT方法时,创建一个领料单并将其添加到Web Api端的数据库中

当我删除
[HttpPost]
时,错误更改为:

找不到方法

方法不允许

这是我的代码:

客户端

public async Task<bool> AddTarefa(ListasPicking listaPickingAdd) 
{
    String listaparaAdicionar = listaPickingAdd.idLista + ";" + listaPickingAdd.IDordemVenda + ";" + listaPickingAdd.peso + ";" + listaPickingAdd.itens;
    HttpResponseMessage response = await cliente.PutAsJsonAsync("api/ListasPicking/", listaparaAdicionar);

    return response.IsSuccessStatusCode;
}
我尝试了一些与这类问题相关的解决方案,但出现了相同的错误

Web.config


演示
管理员
123456
更新

当我重建我的解决方案时,我得到如下几行:

文件\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1964,5): 警告MSB3270:处理器之间存在不匹配 正在构建的项目“MSIL”和处理器的体系结构 参考“Interop.ICrmDS800”、“x86”的体系结构。这 不匹配可能导致运行时失败。请考虑更改 通过 配置管理器,以便调整处理器架构 在项目和引用之间,或者依赖于 使用与目标应用程序匹配的处理器体系结构进行引用 项目的处理器体系结构

有什么问题吗?Interop.dll是我在项目中包含的一个文件,用于使用外部程序获取有关销售订单的信息

解决方案


“ListaPicking”模型中缺少[Key]注释,我正在传递一个带有默认值的DateTime变量。。。例如,我只更改为DateTime。现在它开始工作:)

您调用
PutAsJsonAsync
,但当它需要一个表示json的字符串时,您将一个非json字符串作为数据参数传递

从文件中:

将PUT请求作为异步操作发送到指定Uri,并将给定值序列化为JSON


要使其工作,请调用
PutAsync

只需发送整个模型即可

public async Task<bool> AddTarefa(ListasPicking listaPickingAdd) {

    var response = await cliente.PutAsJsonAsync("api/ListasPicking/", listaPickingAdd);

    return response.IsSuccessStatusCode;        
}

在尝试访问其成员之前,请检查结果.Length。另外,如果您调试代码并在问题中添加服务器端的异常,也会更好。现在告诉您字符串不能转换为http.content。@bullprog-您应该能够在标题中设置
内容类型,使用
应用程序/x-www-form-urlcoded
,并在字符串值前面加上
=
。你看,我还是有错误。。。请看我的更新@bullprog遵循错误消息中提供的明确说明警告消失,但我得到了相同的错误“内部服务器错误”。对不起,浪费了你的时间@bullprog您需要精确地缩小错误发生的范围。它是在调用操作还是在到达操作之前发生错误?如果它正在到达操作,那么在那里放一个try-catch,看看在试图保存记录时是否抛出了错误。这可能会为您提供更多有关导致错误的原因的信息。@bullprog至少您已经缩小了抛出错误的范围。错误是什么。它必须与您的数据存储(实体框架)相关
public async Task<bool> AddTarefa(ListasPicking listaPickingAdd) {

    var response = await cliente.PutAsJsonAsync("api/ListasPicking/", listaPickingAdd);

    return response.IsSuccessStatusCode;        
}
[ResponseType(typeof (ListasPicking))]
[HttpPut]
public  IHttpActionResult PutLista ([FromBody] ListasPicking novaLista) {
    if(!ModelState.IsValid) {
        return BadRequest(ModelState);
    }

    primContext.ListasPickingGet.Add(novaLista);
    primContext.SaveChanges();

    return StatusCode(HttpStatusCode.Created);
}