与OData的补丁asp.net的MVC Web API部分更新 [英] asp.net mvc web api partial update with OData Patch

查看:237
本文介绍了与OData的补丁asp.net的MVC Web API部分更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用HttpPatch部分地更新对象。为了得到我使用达美和OData的修补方法(这里提到的工作:<一href=\"http://stackoverflow.com/questions/14177676/whats-the-currently-recommended-way-of-performing-partial-updates-with-web-api\">What's使用Web API进行部分更新的目前推荐的方法是什么?)。一切似乎都工作正常,但发现映射器是大小写敏感的;当以下对象传递属性都得到更新值:

  {
  标题:新名称在这儿,
  SHORTDESCRIPTION:新建文本放在这里
}

但是当我通过较低或驼峰性质相同的对象,补丁不起作用 - 新价值是不会通过,所以它看起来像有一个与deserialisation和属性映射,即一个问题:SHORTDESCRIPTION为SHORTDESCRIPTION。

有一个配置部分,将使用补丁忽略大小写?

FYI:

在输出我有使用以下格式驼峰特性(以下REST最佳实践):

  //格式化
JsonSerializerSettings JSS =新JsonSerializerSettings();
jss.ContractResolver =新CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SerializerSettings = JSS;//输出示例
{
  头衔:第一,
  SHORTDESCRIPTION:第一篇文章!
}

我的模型类是不过follwing C#/。NET格式约定:

 公共类条目{
  公共字符串名称{搞定;组;}
  公共字符串SHORTDESCRIPTION {搞定;组;}
  //将code的其余部分省略
}


解决方案

简短的回答,没有,没有配置选项撤消的情况下敏感性(据我所知)

龙回答:我今天有同样的问题,因为你,这是我的工作如何围绕它。结果
我发现它令人难以置信的烦人,它必须是大小写敏感的,因此我决定与整个OData的部分做掉,因为它是我们滥用一个巨大的图书馆....结果

这个实现的例子可以在我的github github上

中找到


我决定实现我自己的补丁的方法,因为这是我们实际上是缺少肌肉。我创建了以下抽象类:

 公共抽象类为MyModel
{
    公共无效补丁(对象U)
    {
        VAR道具中,p = this.GetType()的GetProperties()
                    让ATTR = p.GetCustomAttribute(typeof运算(NotPatchableAttribute))
                    其中attr == NULL
                    器选择p;
        的foreach(在道具VAR道具)
        {
            VAR VAL = prop.GetValue(本,NULL);
            如果(VAL!= NULL)
                prop.SetValue(U,VAL);
        }
    }
}


然后,我让我所有的模型类从*为MyModel继承*。记下我使用* *让行,我会以后excplain。所以,现在你可以从你的控制器操作删除三角洲,只是让它再次入境,与put方法。例如

 公共IHttpActionResult PatchUser(INT ID,输入新条目)

您仍然可以使用补丁的方法,你习惯的方式:

  VAR进入= dbContext.Entries.SingleOrDefault(P =&GT; p.ID == ID);
newEntry.Patch(输入);
dbContext.SaveChanges();

现在,让我们回到行

 让ATTR = p.GetCustomAttribute(typeof运算(NotPatchableAttribute))

我发现它,只是任何财产将能够与补丁请求更新存在安全隐患。例如,您现在可能希望一个ID是由贴片changeble。我创建了一个自定义属性与装饰我的属性。该NotPatchable属性:

 公共类NotPatchableAttribute:属性{}

您可以使用它就像任何其他的属性:

 公共类用户:为MyModel
{
    [NotPatchable]
    公众诠释ID {搞定;组; }
    [NotPatchable]
    公共BOOL删除{搞定;组; }
    公共字符串名字{获得;组; }
}

这在此调用的删除,ID属性无法虽然打补丁方法改变了。


我希望这为您解决为好。不要犹豫,发表评论,如果您有任何问题。


我说我的检查道具一个新的MVC项目5的截图。正如你可以看到结果视图填入标题和SHORTDESCRIPTION。

I am using HttpPatch to partially update an object. To get that working I am using Delta and Patch method from OData (mentioned here: What's the currently recommended way of performing partial updates with Web API?). Everything seems to be working fine but noticed that mapper is case sensitive; when the following object is passed the properties are getting updated values:

{
  "Title" : "New title goes here",
  "ShortDescription" : "New text goes here"
}

But when I pass the same object with lower or camel-case properties, Patch doesn't work - new value is not going through, so it looks like there is a problem with deserialisation and properties mapping, ie: "shortDescription" to "ShortDescription".

Is there a config section that will ignore case sensitivity using Patch?

FYI:

On output I have camel-case properties (following REST best practices) using the following formatter:

//formatting
JsonSerializerSettings jss = new JsonSerializerSettings();
jss.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SerializerSettings = jss;

//sample output
{
  "title" : "First",
  "shortDescription" : "First post!"
}

My model classes however are follwing C#/.NET formatting conventions:

public class Entry {
  public string Title { get; set;}
  public string ShortDescription { get; set;}
  //rest of the code omitted
}

解决方案

Short answer, No there is no config option to undo the case sensitiveness (as far as i know)

Long answer: I had the same problem as you today, and this is how i worked around it.
I found it incredibly annoying that it had to be case sensitive, thus i decided to do away with the whole oData part, since it is a huge library that we are abusing....

An example of this implementation can be found at my github github

I decided to implement my own patch method, since that is the muscle that we are actually lacking. I created the following abstract class:

public abstract class MyModel
{
    public void Patch(Object u)
    {
        var props = from p in this.GetType().GetProperties()
                    let attr = p.GetCustomAttribute(typeof(NotPatchableAttribute))
                    where attr == null
                    select p;
        foreach (var prop in props)
        {
            var val = prop.GetValue(this, null);
            if (val != null)
                prop.SetValue(u, val);
        }
    }
}

Then i make all my model classes inherit from *MyModel*. note the line where i use *let*, i will excplain that later. So now you can remove the Delta from you controller action, and just make it Entry again, as with the put method. e.g.

public IHttpActionResult PatchUser(int id, Entry newEntry)

You can still use the patch method the way you used to:

var entry = dbContext.Entries.SingleOrDefault(p => p.ID == id);
newEntry.Patch(entry);
dbContext.SaveChanges();

Now, let's get back to the line

let attr = p.GetCustomAttribute(typeof(NotPatchableAttribute))

I found it a security risk that just any property would be able to be updated with a patch request. For example, you might now want the an ID to be changeble by the patch. I created a custom attribute to decorate my properties with. the NotPatchable attribute:

public class NotPatchableAttribute : Attribute {}

You can use it just like any other attribute:

public class User : MyModel
{
    [NotPatchable]
    public int ID { get; set; }
    [NotPatchable]
    public bool Deleted { get; set; }
    public string FirstName { get; set; }
}

This in this call the Deleted and ID properties cannot be changed though the patch method.

I hope this solve it for you as well. Do not hesitate to leave a comment if you have any questions.

I added a screenshot of me inspecting the props in a new mvc 5 project. As you can see the Result view is populated with the Title and ShortDescription.

这篇关于与OData的补丁asp.net的MVC Web API部分更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆