OData 补丁未更新数据库
OData Patch is not updating the database
我有一个 WebApi 2 控制器。我想在其中一个控制器上使用 OData 补丁。这是我到目前为止所做的。
我在 WebApiConfig 中添加了以下行
config.MapODataServiceRoute("odata", "odata", GenerateEdmModle());
private static Microsoft.OData.Edm.IEdmModel GenerateEdmModle()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Auth>("Auths");
return builder.GetEdmModel();
}
然后在控制器中,这就是我尝试使用补丁方法的方式
[HttpPatch]
public async Task<IHttpActionResult> PatchAuth(int id, Delta<Auth> value)
{
var auth = await db.Auth.FindAsync(id);
if (auth == null) return NotFound();
System.Diagnostics.Debug.WriteLine(auth.direction, auth.id);
System.Diagnostics.Debug.WriteLine("Patching");
try
{
value.Patch(auth);
await db.SaveChangesAsync();
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
return InternalServerError(e);
}
return Ok(value);
}
这是我从 angular 服务
发送它的方式
// patch auth
service.patchAuth = function (authId, auth) {
var request = $http({
method: 'PATCH',
url: baseUrl + 'api/Auths',
data: JSON.stringify(auth),
params: { id: authId },
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
return (request.then(handleSuccess, handleError));
}
这是我在 Fiddler 中看到的
我看到控制器找到了 Patch Method,似乎它正在尝试更新,但该值永远不会更新。
我也在value.Patch(auth)
下了断点,检查了changedProperties
,但是什么也没有。我一直在努力找出造成这种情况的原因,但没有任何线索。
您指定 application/x-www-form-urlencoded
作为您的内容类型。相反,您必须使用 application/json
.
当您指定 application/x-www-form-urlencoded
时,调用仍会路由到正确的补丁程序处理程序(如您的情况),但是 Web.Api 不会将更改的属性传递到 Delta<T>
。
当您在 Fiddler 中检查 raw HTTP 请求时,您的调用应该更像这样:
PATCH http://www.example.com/api/Auths(5) HTTP/1.1
Content-Type: application/json
Host: www.example.com
Content-Length: 20
{ "id" : 123456789 }
我有一个 WebApi 2 控制器。我想在其中一个控制器上使用 OData 补丁。这是我到目前为止所做的。
我在 WebApiConfig 中添加了以下行
config.MapODataServiceRoute("odata", "odata", GenerateEdmModle());
private static Microsoft.OData.Edm.IEdmModel GenerateEdmModle()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Auth>("Auths");
return builder.GetEdmModel();
}
然后在控制器中,这就是我尝试使用补丁方法的方式
[HttpPatch]
public async Task<IHttpActionResult> PatchAuth(int id, Delta<Auth> value)
{
var auth = await db.Auth.FindAsync(id);
if (auth == null) return NotFound();
System.Diagnostics.Debug.WriteLine(auth.direction, auth.id);
System.Diagnostics.Debug.WriteLine("Patching");
try
{
value.Patch(auth);
await db.SaveChangesAsync();
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
return InternalServerError(e);
}
return Ok(value);
}
这是我从 angular 服务
发送它的方式// patch auth
service.patchAuth = function (authId, auth) {
var request = $http({
method: 'PATCH',
url: baseUrl + 'api/Auths',
data: JSON.stringify(auth),
params: { id: authId },
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
return (request.then(handleSuccess, handleError));
}
这是我在 Fiddler 中看到的
我看到控制器找到了 Patch Method,似乎它正在尝试更新,但该值永远不会更新。
我也在value.Patch(auth)
下了断点,检查了changedProperties
,但是什么也没有。我一直在努力找出造成这种情况的原因,但没有任何线索。
您指定 application/x-www-form-urlencoded
作为您的内容类型。相反,您必须使用 application/json
.
当您指定 application/x-www-form-urlencoded
时,调用仍会路由到正确的补丁程序处理程序(如您的情况),但是 Web.Api 不会将更改的属性传递到 Delta<T>
。
当您在 Fiddler 中检查 raw HTTP 请求时,您的调用应该更像这样:
PATCH http://www.example.com/api/Auths(5) HTTP/1.1
Content-Type: application/json
Host: www.example.com
Content-Length: 20
{ "id" : 123456789 }