c# - 我可以在保存更改之前验证实体吗?

标签 c# json wcf-data-services odata

我有一个非常简单的 WCF 数据服务应用程序,并且正在执行一些基本的 CRUD 操作。我在正在更改的实体集上有一个 ChangeInterceptor,但 ChangeInterceptor 中的对象是数据库中的当前状态,而不是 HTTP PUT 中发送的内容。 有没有办法在保存对象之前验证其属性?

这是我的 ChangeInterceptor:

[ChangeInterceptor("People")]
public void OnChangePerson(Person personChanging, UpdateOperations updateOperations) {
    switch (updateOperations) {
        case UpdateOperations.Change:
            // personChanging is the database version here, not the changed version.
            break;
        default:
            break;
    }
}

这是我的客户端代码(jQuery):

var data = {
    FirstName: "NewFN",
    LastName: "NewLN"
};
$.ajax({
    type: "PUT",
    url: serviceUrl + "/People(" + personID + ")",
    contentType: "application/json",
    dataType: "json",
    data: JSON.stringify(data),
    success: function (data) {
        alert("Success!");
    },
    error: function (error) {
        alert("An error occured");
    }
});

这是发送到服务器的 JSON: alt text

这是收到消息时的ChangeInterceptor: alt text

我已在此处上传了该项目的代码:http://andyjmay.com/test/2921612/ODataTest.zip

最佳答案


我下载了您的示例,重现了您的问题,并且目前能够使用此解决方法查看最新更新的值。
当我在内部调查这个问题时,您可以更改代码以使用 Merge 动词而不是 PUT 吗?
通过此更改,当您通过 jQuery 客户端更新值时,您现在应该能够看到传递到 ChangeInterceptors 的最新实体值。

$.ajax({<br/> beforeSend: function (xhrObj) {<br/> xhrObj.setRequestHeader("X-Http-Method", "MERGE");<br/> },<br/> type: "POST",<br/> url: serviceUrl + "/People(" + personID + ")",<br/> contentType: "application/json",<br/> dataType: "json",<br/> data: JSON.stringify(data),<br/> success: function (data) {<br/> GetAllPeople();<br/> },<br/> error: function (error) {<br/> alert(error);<br/> }<br/> });

关于c# - 我可以在保存更改之前验证实体吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3781705/

相关文章:

c# - 在 Visual Studio 2010-沙盒解决方案中添加对第 3 方 DLL 的引用

java - Boilerpipe - 如何输出 JSON?

odata - SingleResult<T> 无法在 Web API 中按键查询时序列化

jquery - 是否有 ADO.NET 数据服务 jQuery 插件?

c# - 使用内部应用程序的模型将公共(public)表单提交到安全的内部应用程序

c# - 可以使用 EF 迁移更新生产数据库吗?

c# - UdpClient.EnableBroadcast 值无效

java - 将 @Id Marshall 转换为 JSON,同时保留 Java 8 时间格式

c# - 代码分析规则 CA1062 行为

c# - JSON.NET 在解析集合时忽略空值