Azure Functions HTTP 输出绑定Azure Functions HTTP output bindings

通过 HTTP 输出绑定响应 HTTP 请求发送者。Use the HTTP output binding to respond to the HTTP request sender. 此绑定需要使用 HTTP 触发器,利用此绑定,可以自定义与触发器请求相关联的响应。This binding requires an HTTP trigger and allows you to customize the response associated with the trigger's request.

HTTP 触发函数的默认返回值如下:The default return value for an HTTP-triggered function is:

  • HTTP 204 No Content,在 Functions 2.x 及更高版本中为空主体HTTP 204 No Content with an empty body in Functions 2.x and higher
  • HTTP 200 OK,在 Functions 1.x 中为空主体HTTP 200 OK with an empty body in Functions 1.x

配置Configuration

下表解释了在 function.json 文件中设置的绑定配置属性。The following table explains the binding configuration properties that you set in the function.json file. 在 C# 类库中,没有与这些 function.json 属性对应的特性。For C# class libraries, there are no attribute properties that correspond to these function.json properties.

属性Property 说明Description
typetype 必须设置为 httpMust be set to http.
directiondirection 必须设置为 outMust be set to out.
namename 在响应的函数代码中使用的变量名称,或者 $return 以使用返回值。The variable name used in function code for the response, or $return to use the return value.

使用情况Usage

若要发送 HTTP 响应,请使用语言标准响应模式。To send an HTTP response, use the language-standard response patterns. 在 C# 或 C# 脚本中,使函数返回类型为 IActionResultTask<IActionResult>In C# or C# script, make the function return type IActionResult or Task<IActionResult>. 在 C# 中,不需要返回值属性。In C#, a return value attribute isn't required.

有关示例响应,请参阅触发器示例For example responses, see the trigger example.

host.json 设置host.json settings

本部分介绍版本 2.x 及更高版本中可用于此绑定的全局配置设置。This section describes the global configuration settings available for this binding in versions 2.x and higher. 下面的示例 host.json 文件仅包含此绑定的 2.x 版及更高版本设置。The example host.json file below contains only the version 2.x+ settings for this binding. 若要详细了解 2.x 版及更高版本中的全局配置设置,请参阅 Azure Functions 的 host.json 参考For more information about global configuration settings in versions 2.x and beyond, see host.json reference for Azure Functions.

备注

有关 Functions 1.x 中 host.json 的参考,请参阅 Azure Functions 1.x 的 host.json 参考For a reference of host.json in Functions 1.x, see host.json reference for Azure Functions 1.x.

{
    "extensions": {
        "http": {
            "routePrefix": "api",
            "maxOutstandingRequests": 200,
            "maxConcurrentRequests": 100,
            "dynamicThrottlesEnabled": true,
            "hsts": {
                "isEnabled": true,
                "maxAge": "10"
            },
            "customHeaders": {
                "X-Content-Type-Options": "nosniff"
            }
        }
    }
}
属性Property 默认Default 说明Description
customHeaderscustomHeaders none 使你可以在 HTTP 响应中设置自定义标头。Allows you to set custom headers in the HTTP response. 前面的示例将 X-Content-Type-Options 标头添加到响应中,以避免内容类型探查。The previous example adds the X-Content-Type-Options header to the response to avoid content type sniffing.
dynamicThrottlesEnableddynamicThrottlesEnabled true*true* 启用后,此设置将导致请求处理管道定期检查系统性能计数器(例如 connections/threads/processes/memory/cpu/etc),如果这些计数器中的任何一个超过内置高阈值 (80%),则请求将被拒绝并返回 429 "Too Busy" 响应,直到计数器恢复正常水平为止。When enabled, this setting causes the request processing pipeline to periodically check system performance counters like connections/threads/processes/memory/cpu/etc and if any of those counters are over a built-in high threshold (80%), requests will be rejected with a 429 "Too Busy" response until the counter(s) return to normal levels.
*消耗计划中的默认值为 true*The default in a Consumption plan is true. 专用计划中的默认值为 falseThe default in a Dedicated plan is false.
hstshsts 未启用not enabled isEnabled 设置为 true 时,将强制执行 .NET Core 的 HTTP 严格传输安全性 (HSTS) 行为,如 HstsOptions中所定义。When isEnabled is set to true, the HTTP Strict Transport Security (HSTS) behavior of .NET Core is enforced, as defined in the HstsOptions class. 上面的示例还将 maxAge 属性设置为 10 天。The above example also sets the maxAge property to 10 days. hsts 支持的属性包括:Supported properties of hsts are:
属性Property说明Description
excludedHostsexcludedHosts未添加 HSTS 标头的主机名的字符串数组。A string array of host names for which the HSTS header isn't added.
includeSubDomainsincludeSubDomains布尔值,指示是否启用了 Strict-Transport-Security 标头的 includeSubDomain 参数。Boolean value that indicates whether the includeSubDomain parameter of the Strict-Transport-Security header is enabled.
maxAgemaxAge定义 Strict-Transport-Security 标头的 max-age 参数的字符串。String that defines the max-age parameter of the Strict-Transport-Security header.
preloadpreload布尔值,指示是否启用了 Strict-Transport-Security 标头的 preload 参数。Boolean that indicates whether the preload parameter of the Strict-Transport-Security header is enabled.
maxConcurrentRequestsmaxConcurrentRequests 100*100* 并行执行的 HTTP 函数数目上限。The maximum number of HTTP functions that are executed in parallel. 使用此值可以控制并发性,从而帮助管理资源利用率。This value allows you to control concurrency, which can help manage resource utilization. 例如,某个 HTTP 函数可能使用了大量系统资源(内存/CPU/套接字),从而在并发性过高时导致问题。For example, you might have an HTTP function that uses a large number of system resources (memory/cpu/sockets) such that it causes issues when concurrency is too high. 或者,如果某个函数向第三方服务发出了出站请求,则可能需要限制这些调用的速率。Or you might have a function that makes outbound requests to a third-party service, and those calls need to be rate limited. 在这种情况下,应用限制可能有帮助。In these cases, applying a throttle here can help.
*消耗计划的默认值为 100。*The default for a Consumption plan is 100. 专用计划的默认值为“无限”(-1)。The default for a Dedicated plan is unbounded (-1).
maxOutstandingRequestsmaxOutstandingRequests 200*200* 在任意给定时间搁置的未完成请求数上限。The maximum number of outstanding requests that are held at any given time. 此限制包括已排队但尚未开始执行的请求,以及正在执行的所有请求。This limit includes requests that are queued but have not started executing, as well as any in progress executions. 超出此限制的任何传入请求将被拒绝,并返回 429“太忙”响应。Any incoming requests over this limit are rejected with a 429 "Too Busy" response. 允许调用方使用基于时间的重试策略,还可帮助控制最大请求延迟。That allows callers to employ time-based retry strategies, and also helps you to control maximum request latencies. 此设置仅控制脚本宿主执行路径中发生的排队。This only controls queuing that occurs within the script host execution path. 其他队列(例如 ASP.NET 请求队列)仍有效,不受此设置的影响。Other queues such as the ASP.NET request queue will still be in effect and unaffected by this setting.
*消耗计划的默认值为 200。*The default for a Consumption plan is 200. 专用计划的默认值为“无限”(-1)。The default for a Dedicated plan is unbounded (-1).
routePrefixroutePrefix apiapi 应用到所有路由的路由前缀。The route prefix that applies to all routes. 使用空字符串可删除默认前缀。Use an empty string to remove the default prefix.

后续步骤Next steps