Azure 函数 Return Http 响应中的字符串
Azure Function Return String in Http Response
这看起来很简单,但我想语法/库已经改变了很多次,找到一个样本就像试图在一大碗肉酱中追踪一条意大利面条的整个路径。
我有以下 C# dotnet 5 Azure 函数:
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequestData req,
ILogger log)
string result = "for the love of god, I just want to return this string"
我想 return 带有 200 响应代码的“结果”字符串。
我尝试了以下方法
return result != null ? New OkObjectResult(result) : new StatusCodeResult(500);
但不是 return 字符串,而是 returns:
{
"Value": "content of result variable here",
"Formatters": [],
"ContentTypes": [],
"DeclaredType": null,
"StatusCode": 200
}
有什么想法吗?
使用新的 'isolated' 模型,您的应用程序现在在函数宿主的单独进程中运行。 Microsoft 在 运行 进程外时提供 list of benefits,主要好处是能够为函数宿主提供不同版本的 dll,而不会在运行时发生冲突(这是我必须过去经常打交道)。
由于您的应用程序 运行 作为主机的一个单独进程,您不能只 return 一个对象引用作为函数的输出。该对象必须被序列化,以便被发送回主机进程。这意味着输入和输出绑定不能是功能对象,只能是可序列化数据,所以 most of the input and output bindings (including IActionResult) have been simplified. By default, if you try to return an object, it will be serialized as json, which is what you are seeing when you return new OkObjectResult(result)
. If you want to serialize it explicitly, you need to return HttpResponseData
,而且比以前复杂得多:
[Function("HttpFunction")]
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestData req)
{
var json = JsonConvert.SerializeObject("for the love of god, I just want to return this string");
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/json; charset=utf-8");
response.WriteString(json);
return response;
}
这看起来很简单,但我想语法/库已经改变了很多次,找到一个样本就像试图在一大碗肉酱中追踪一条意大利面条的整个路径。
我有以下 C# dotnet 5 Azure 函数:
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequestData req,
ILogger log)
string result = "for the love of god, I just want to return this string"
我想 return 带有 200 响应代码的“结果”字符串。
我尝试了以下方法
return result != null ? New OkObjectResult(result) : new StatusCodeResult(500);
但不是 return 字符串,而是 returns:
{
"Value": "content of result variable here",
"Formatters": [],
"ContentTypes": [],
"DeclaredType": null,
"StatusCode": 200
}
有什么想法吗?
使用新的 'isolated' 模型,您的应用程序现在在函数宿主的单独进程中运行。 Microsoft 在 运行 进程外时提供 list of benefits,主要好处是能够为函数宿主提供不同版本的 dll,而不会在运行时发生冲突(这是我必须过去经常打交道)。
由于您的应用程序 运行 作为主机的一个单独进程,您不能只 return 一个对象引用作为函数的输出。该对象必须被序列化,以便被发送回主机进程。这意味着输入和输出绑定不能是功能对象,只能是可序列化数据,所以 most of the input and output bindings (including IActionResult) have been simplified. By default, if you try to return an object, it will be serialized as json, which is what you are seeing when you return new OkObjectResult(result)
. If you want to serialize it explicitly, you need to return HttpResponseData
,而且比以前复杂得多:
[Function("HttpFunction")]
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestData req)
{
var json = JsonConvert.SerializeObject("for the love of god, I just want to return this string");
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/json; charset=utf-8");
response.WriteString(json);
return response;
}