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

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

        <bdo id='B5Aqc'></bdo><ul id='B5Aqc'></ul>
      1. 我的 HTTP 触发 Azure 函数如何将请求的参数直接传递给 Run 方法?

        时间:2023-10-05
        • <bdo id='x1gxs'></bdo><ul id='x1gxs'></ul>

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

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

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

                  <tfoot id='x1gxs'></tfoot>
                    <tbody id='x1gxs'></tbody>
                  本文介绍了我的 HTTP 触发 Azure 函数如何将请求的参数直接传递给 Run 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下由 HTTP 调用触发的 Azure 函数:

                  公共静态类 MyAzureFunction{[FunctionName("api/v1/resource/")]公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function,get")]HttpRequestMessage 请求,ILogger 记录器){//从请求中提取查询字符串参数...}}

                  我希望将参数自动传递给 Run 方法,就像使用 ASP.NET Core Web API 完成的那样,而不是必须从请求本身中提取它们并解析它们.

                  这是我想要得到的示例:

                  [FunctionName("api/v1/resource/{resourceId}")]公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function,get")]HttpRequestMessage 请求,ILogger 记录器,int resourceId){//...}

                  或者,在进行 POST 时:

                  [FunctionName("api/v1/resource/")]公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function,post")]HttpRequestMessage 请求,ILogger 记录器,[FromBody] SomeEntityModel entityModel){//...}

                  解决方案

                  参考 Azure Functions HTTP 触发器和绑定:自定义 HTTP 终结点

                  对于 GET,您可以使用触发器上的 Route 属性属性为函数设置路由模板

                  <块引用>

                  定义路由模板,控制您的函数响应的请求 URL.如果没有提供,则默认值为 <functionname>

                  这允许函数代码支持地址中的参数,如{resourceId}.

                  您可以将任何Web API 路由约束与您的参数一起使用.

                  例如

                  Route = "v1/resource/{resourceId:int}"

                  <块引用>

                  默认情况下,所有函数路由都以前缀api

                  以下使用带约束的参数

                  [FunctionName("MyFunctionName")]公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/resource/{resourceId:int}")]HttpRequestMessage 请求,ILogger 记录器,诠释资源ID){//...}

                  到目前为止,我还没有找到关于使用 FromBody 属性的详细信息,但是下面的引用似乎很有成效

                  <块引用>

                  对于 C# 和 F# 函数,您可以将触发器输入的类型声明为 HttpRequestMessage 或自定义类型.如果您选择 HttpRequestMessage,您将获得对请求对象的完全访问权限.对于自定义类型,运行时会尝试解析 JSON 请求正文以设置对象属性.

                  注意:强调我的

                  应该涵盖哪些

                  [FunctionName("MyPOSTFunction")]公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1/resource/" )]SomeEntityModel 实体模型,ILogger 记录器){//...}

                  I have the following Azure Function triggered by an HTTP call:

                  public static class MyAzureFunction
                  {
                      [FunctionName("api/v1/resource/")]
                      public static async Task Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage request, ILogger logger)
                      {
                          // Extract query string params from the request...
                      } 
                  }
                  

                  I would like to have the parameters to be automatically passed to the Run method, the same way it is being done with ASP.NET Core Web API, instead of having to extract them from the request itself and parse them.

                  Here is an example of what I would like to get:

                  [FunctionName("api/v1/resource/{resourceId}")]
                  public static async Task Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage request, ILogger logger, int resourceId)
                  {
                       // ...
                  } 
                  

                  Or, when doing a POST:

                  [FunctionName("api/v1/resource/")]
                  public static async Task Run([HttpTrigger(AuthorizationLevel.Function, "post")]HttpRequestMessage request, ILogger logger, [FromBody] SomeEntityModel entityModel)
                  {
                           // ...
                  }
                  

                  解决方案

                  Reference Azure Functions HTTP triggers and bindings: Customize the HTTP endpoint

                  For the GET you can use the Route attribute property on the trigger to set a route template for the function

                  Defines the route template, controlling to which request URLs your function responds. The default value if none is provided is <functionname>

                  This allows the function code to support parameters in the address, like {resourceId}.

                  You can use any Web API Route Constraint with your parameters.

                  for example

                  Route = "v1/resource/{resourceId:int}"
                  

                  By default, all function routes are prefixed with api

                  The following makes use of the parameter with constraints

                  [FunctionName("MyFunctionName")]
                  public static async Task Run(
                      [HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/resource/{resourceId:int}")]
                      HttpRequestMessage request, 
                      ILogger logger, 
                      int resourceId) {
                      // ...
                  }
                  

                  So far I have not been able to find details about the use of FromBody attribute, but the following quote seems fruitful

                  For C# and F# functions, you can declare the type of your trigger input to be either HttpRequestMessage or a custom type. If you choose HttpRequestMessage, you get full access to the request object. For a custom type, the runtime tries to parse the JSON request body to set the object properties.

                  note: emphasis mine

                  Which should cover

                  [FunctionName("MyPOSTFunction")]
                  public static async Task Run(
                      [HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1/resource/" )]
                      SomeEntityModel entityModel, 
                      ILogger logger) {
                      // ...
                  }
                  

                  这篇关于我的 HTTP 触发 Azure 函数如何将请求的参数直接传递给 Run 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Azure Functions - 共享类 下一篇:预编译的 Azure 函数和 CloudTable 绑定输出不起作用

                  相关文章

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

                    <tfoot id='Wjivg'></tfoot>
                  1. <legend id='Wjivg'><style id='Wjivg'><dir id='Wjivg'><q id='Wjivg'></q></dir></style></legend>

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

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