• <small id='N3l8E'></small><noframes id='N3l8E'>

    • <bdo id='N3l8E'></bdo><ul id='N3l8E'></ul>

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

        在 asp.net mvc core 中绑定 Guid 参数

        时间:2023-07-10

          <tbody id='RKWcX'></tbody>

            <legend id='RKWcX'><style id='RKWcX'><dir id='RKWcX'><q id='RKWcX'></q></dir></style></legend>

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

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

                • <bdo id='RKWcX'></bdo><ul id='RKWcX'></ul>
                • 本文介绍了在 asp.net mvc core 中绑定 Guid 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想将 Guid 参数绑定到我的 ASP.NET MVC Core API:

                  I want to bind a Guid parameter to my ASP.NET MVC Core API:

                  [FromHeader] Guid id
                  

                  但它始终为空.如果我将参数更改为字符串并手动从字符串中解析 Guid,它可以工作,所以我认为它没有将 Guid 检测为可转换类型.

                  but it's always null. If I change the parameter to a string and parse the Guid from the string manually it works, so I think it's not detecting Guid as a convertable type.

                  在文档中说

                  在 MVC 中,简单类型是任何 .NET 原始类型或带有字符串类型转换器的类型.

                  In MVC simple types are any .NET primitive type or type with a string type converter.

                  Guid 有一个类型转换器 (GuidConverter) 但也许 ASP.NET MVC Core 不知道.

                  There is a type converter for Guids (GuidConverter) but maybe ASP.NET MVC Core doesn't know about it.

                  有谁知道如何将 Guid 参数与 ASP.NET MVC Core 绑定或如何告诉它使用 GuidConverter?

                  Does anyone know how to bind a Guid parameter with ASP.NET MVC Core or how to tell it to use GuidConverter?

                  推荐答案

                  我刚刚发现,ASP Core 基本上只支持将标头值绑定到字符串和字符串集合!(而路由值、查询字符串和正文的绑定支持任何复杂类型)

                  I have just found out that basically ASP Core only supports binding header values to strings and collections of strings! (whereas binding from route values, query string and body supports any complex type)

                  您可以检查 HeaderModelBinderProvider Github 中的源码 自己看看:

                  You can check the HeaderModelBinderProvider source in Github and see for yourself:

                  public IModelBinder GetBinder(ModelBinderProviderContext context)
                  {
                      if (context == null)
                      {
                          throw new ArgumentNullException(nameof(context));
                      }
                  
                      if (context.BindingInfo.BindingSource != null &&
                              context.BindingInfo.BindingSource.CanAcceptDataFrom(BindingSource.Header))
                      {
                          // We only support strings and collections of strings. Some cases can fail
                          // at runtime due to collections we can't modify.
                          if (context.Metadata.ModelType == typeof(string) ||
                              context.Metadata.ElementType == typeof(string))
                          {
                              return new HeaderModelBinder();
                          }
                      }
                  
                      return null;
                  }
                  

                  我已经提交了 新问题,但同时我建议你要么绑定到字符串或创建您自己的特定模型绑定器(将 [FromHeader][ModelBinder] 组合到您自己的绑定器中)

                  I have submitted a new issue, but in the meantime I would suggest you either bind to a string or create your own specific model binder (something that combines [FromHeader] and [ModelBinder] into your own binder)

                  编辑

                  示例模型绑定器可能如下所示:

                  The sample model binder could look like this:

                  public class GuidHeaderModelBinder : IModelBinder
                  {
                      public Task BindModelAsync(ModelBindingContext bindingContext)
                      {
                          if (bindingContext.ModelType != typeof(Guid)) return Task.CompletedTask;
                          if (!bindingContext.BindingSource.CanAcceptDataFrom(BindingSource.Header)) return Task.CompletedTask;
                  
                          var headerName = bindingContext.ModelName;
                          var stringValue = bindingContext.HttpContext.Request.Headers[headerName];
                          bindingContext.ModelState.SetModelValue(bindingContext.ModelName, stringValue, stringValue);
                  
                          // Attempt to parse the guid                
                          if (Guid.TryParse(stringValue, out var valueAsGuid))
                          {
                              bindingContext.Result = ModelBindingResult.Success(valueAsGuid);
                          }
                  
                          return Task.CompletedTask;
                      }
                  }
                  

                  这是一个使用它的例子:

                  And this would be an example using it:

                  public IActionResult SampleAction(
                      [FromHeader(Name = "my-guid")][ModelBinder(BinderType = typeof(GuidHeaderModelBinder))]Guid foo)
                  {
                      return Json(new { foo });
                  }
                  

                  您可以尝试一下,例如在浏览器中使用 jquery:

                  Which you can try, for example with jquery in the browser:

                  $.ajax({
                    method: 'GET',
                    headers: { 'my-guid': '70e9dfda-4982-4b88-96f9-d7d284a10cb4' }, 
                    url: '/home/sampleaction'
                  });
                  

                  这篇关于在 asp.net mvc core 中绑定 Guid 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ASP.NET Core 发布数组对象 JSON 下一篇:MVC6 中的 ASP 标识 - 登录路径属性不起作用

                  相关文章

                  1. <i id='wpJAE'><tr id='wpJAE'><dt id='wpJAE'><q id='wpJAE'><span id='wpJAE'><b id='wpJAE'><form id='wpJAE'><ins id='wpJAE'></ins><ul id='wpJAE'></ul><sub id='wpJAE'></sub></form><legend id='wpJAE'></legend><bdo id='wpJAE'><pre id='wpJAE'><center id='wpJAE'></center></pre></bdo></b><th id='wpJAE'></th></span></q></dt></tr></i><div id='wpJAE'><tfoot id='wpJAE'></tfoot><dl id='wpJAE'><fieldset id='wpJAE'></fieldset></dl></div>
                    <legend id='wpJAE'><style id='wpJAE'><dir id='wpJAE'><q id='wpJAE'></q></dir></style></legend>
                  2. <tfoot id='wpJAE'></tfoot>

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

                      • <bdo id='wpJAE'></bdo><ul id='wpJAE'></ul>