c# - Azure函数返回已弃用的API header

标签 c# azure httpresponse azure-functions

我有一个位于代理后面的 Azure 函数。如果返回的对象发生更新,我们希望在一段时间后弃用该函数。我正在尝试使用提供的 in this solution. 来创建包含 HTTP header 中的预期内容的响应

Warning: 299 - "Deprecated API"

我尝试像这样附加 Azure 函数:

 [FunctionName("MyAPI")]
    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function,
        "post", Route = null)]
        HttpRequestMessage req,
        TraceWriter log)
    {            
        object response = await someService.Get();
        if (settingsService.IsDeprecated)
        {
            var httpresponse = req.CreateResponse(HttpStatusCode.OK, response, "application/json");
            httpresponse.Content.Headers.Add("Warning", "299 - Deprecated API");
            return httpresponse;
        }
        return req.CreateResponse(HttpStatusCode.OK, response, "application/json");
    }

我收到异常

Exception while executing function: MyAPI -> Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

如何附加 "Deprecated" status warning在我的 API Http 响应中?

最佳答案

将线路更改为

httpresponse.Headers.Add("Warning", "299 - \"Deprecated API\"");

引号对于遵守格式要求似乎很重要。

关于c# - Azure函数返回已弃用的API header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49637829/

相关文章:

c# - 为什么每个 [TestMethod] 都会多次调用 [TestClass] 的构造函数?

c# - 当您尝试将值设置为只读属性时会发生什么?

sql-server - 需要帮助在 SQL Server 上启用更改数据捕获 (CDC)

azure - 如何禁用 Azure Application Insights 服务器请求分组?

spring-mvc - Spring MVC 为 application/octet-stream 提供 HTTP 406

php - HTTP 响应代码 0 - 网站正在运行

json - XMLHttpRequest 无法加载,但响应显示 200 状态

c# - 如何使用事件更新类中的 TextBox?

c# - 使用 javascript 从另一个按钮触发单击按钮事件

Azure Functions V2 每次发布前都必须停止函数,因为 dll 正忙