• <tfoot id='hhAI5'></tfoot>

        <bdo id='hhAI5'></bdo><ul id='hhAI5'></ul>
      <i id='hhAI5'><tr id='hhAI5'><dt id='hhAI5'><q id='hhAI5'><span id='hhAI5'><b id='hhAI5'><form id='hhAI5'><ins id='hhAI5'></ins><ul id='hhAI5'></ul><sub id='hhAI5'></sub></form><legend id='hhAI5'></legend><bdo id='hhAI5'><pre id='hhAI5'><center id='hhAI5'></center></pre></bdo></b><th id='hhAI5'></th></span></q></dt></tr></i><div id='hhAI5'><tfoot id='hhAI5'></tfoot><dl id='hhAI5'><fieldset id='hhAI5'></fieldset></dl></div>

      1. <small id='hhAI5'></small><noframes id='hhAI5'>

      2. <legend id='hhAI5'><style id='hhAI5'><dir id='hhAI5'><q id='hhAI5'></q></dir></style></legend>

      3. 为什么 BindNever 属性不起作用

        Why BindNever attribute doesn#39;t work(为什么 BindNever 属性不起作用)
      4. <i id='Ydb55'><tr id='Ydb55'><dt id='Ydb55'><q id='Ydb55'><span id='Ydb55'><b id='Ydb55'><form id='Ydb55'><ins id='Ydb55'></ins><ul id='Ydb55'></ul><sub id='Ydb55'></sub></form><legend id='Ydb55'></legend><bdo id='Ydb55'><pre id='Ydb55'><center id='Ydb55'></center></pre></bdo></b><th id='Ydb55'></th></span></q></dt></tr></i><div id='Ydb55'><tfoot id='Ydb55'></tfoot><dl id='Ydb55'><fieldset id='Ydb55'></fieldset></dl></div>
        <tfoot id='Ydb55'></tfoot>
        • <bdo id='Ydb55'></bdo><ul id='Ydb55'></ul>

              <small id='Ydb55'></small><noframes id='Ydb55'>

                1. <legend id='Ydb55'><style id='Ydb55'><dir id='Ydb55'><q id='Ydb55'></q></dir></style></legend>
                    <tbody id='Ydb55'></tbody>
                2. 本文介绍了为什么 BindNever 属性不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我不想在我的 CustomerViewModel 上绑定 Id 属性,所以我添加了一个 [BindNever] 属性,但它不起作用.有什么解决办法?

                  I do not want do bind the Id property on my CustomerViewModel so I added a [BindNever] attribute but it is not working. What could be the solution?

                  我有以下几点:

                  CustomerController.cs

                  // PUT api/customers/5
                  [HttpPut("{id}")]
                  public async Task<IActionResult> Put([FromUri] int id, [FromBody]CustomerViewModel customer)
                  {
                    //Implementation
                  }
                  

                  客户视图模型

                  public class CustomerViewModel
                  {
                      [BindNever]
                      public int Id { get; set; }
                      public string LastName { get; set; }
                      public string FirstName { get; set; }
                      public string Email { get; set; }
                  }
                  

                  如果我输入以下 json .id 属性仍然被绑定

                  If I input the following json . The id property still gets binded

                  {
                    "id": 100,
                    "lastName": "Bruce",
                    "firstName": "Wayne",
                    "email": "bruce@gothamcity.com"
                  }
                  

                  推荐答案

                  这个 博文读起来很有趣,并得出结论,[FromBody] 注释覆盖"了BindBehaviourAttribute(BindNever 是一个简单的特化).该模型由正文中的所有可用数据(在本例中为您的 JSON 数据)填充.

                  This Blog post is an interesting read and concludes that the [FromBody] annotation "overrides" the BindBehaviourAttribute (BindNever is a simple specialization). The model is populated by all data available from the body (your JSON data in this case).

                  我不认为这是直观的,issue 有一个很好的声明关于这个:

                  I do not consider this as intuitive, and the issue has a nice statement about this:

                  [BindRequired] 自定义MVC模型绑定系统.这就是它目的并按设计工作.

                  [BindRequired] customizes the MVC model binding system . That's its purpose and it's working as designed.

                  [FromBody] 将受影响的属性或参数切换到输入格式的不同世界.每个输入格式化程序(例如Json.NET 和一个小型的 MVC 特定包装器)可以被认为是一个具有自己定制的独立系统.模型绑定系统不知道 JSON(或任何其他)反序列化的细节.

                  [FromBody] switches the affected property or parameter into the different world of input formatting. Each input formatter (e.g. Json.NET and a small MVC-specific wrapper) can be considered a separate system with its own customization. The model binding system has no knowledge the details of JSON (or any other) deserialization.

                  经验教训:BindNever 在这种情况下不起作用.

                  Lesson learned: BindNever does not work in this scenario.

                  什么是替代品?

                  解决方案 1:编写一些自定义模型绑定代码.我自己没有做过,但是 在 MVC6 中创建自定义模型绑定器的正确方法是什么? 可能会有所帮助.

                  Solution 1: Writing some custom model binding code. I have not done it myself, but What is the correct way to create custom model binders in MVC6? may help.

                  解决方案 2:比较务实的解决方案

                  Solution 2: Rather pragmatic one

                  也许这个简单(但不是很好)的解决方法可以帮助您:

                  Perhaps this simple (but not very nice) workaround helps you out:

                  [HttpPut("{id}")]
                  public async Task<IActionResult> Put([FromUri] int id, [FromBody]CustomerViewModel customer)
                  {
                      customer.Id = 0;
                      //Implementation
                  }
                  

                  这篇关于为什么 BindNever 属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
                  <i id='iuY6E'><tr id='iuY6E'><dt id='iuY6E'><q id='iuY6E'><span id='iuY6E'><b id='iuY6E'><form id='iuY6E'><ins id='iuY6E'></ins><ul id='iuY6E'></ul><sub id='iuY6E'></sub></form><legend id='iuY6E'></legend><bdo id='iuY6E'><pre id='iuY6E'><center id='iuY6E'></center></pre></bdo></b><th id='iuY6E'></th></span></q></dt></tr></i><div id='iuY6E'><tfoot id='iuY6E'></tfoot><dl id='iuY6E'><fieldset id='iuY6E'></fieldset></dl></div>
                  <tfoot id='iuY6E'></tfoot>
                    <tbody id='iuY6E'></tbody>

                  <small id='iuY6E'></small><noframes id='iuY6E'>

                      <bdo id='iuY6E'></bdo><ul id='iuY6E'></ul>
                          <legend id='iuY6E'><style id='iuY6E'><dir id='iuY6E'><q id='iuY6E'></q></dir></style></legend>