【问题标题】:Azure function to return a specific stringAzure 函数返回特定字符串
【发布时间】:2020-06-06 13:18:45
【问题描述】:

我是 Azure 功能的新手。我有这个简单的.net核心代码如下:

 public static class HttpExample
{
    [FunctionName("HttpExample")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];

        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;

        return name != null
            ? (ActionResult)new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
    }
}

此代码运行良好并返回 Hello, Function。但是,如果我像这样将其更改为“世界”:

   string name = req.Query["World"]; //may be I would like to try multiple strings with space? Just to have fun learning.

它返回我:

  Please pass a name on the query string or in the request body.

如何使用此函数返回任何特定的字符串?

谢谢。

【问题讨论】:

标签: c# .net-core azure-functions


【解决方案1】:

这是您尝试执行的HttpTriggerstring name = req.Query["name"]; 行将在您的请求中找到查询字符串参数name 并从中获取值。

如何使用此函数返回任何特定的字符串?

无论您分配给name query-string 参数,您都会得到响应的字符串。

例如,如果你在浏览器中使用这个url-

http://localhost:7071/api/HttpExample?name=Function

您将收到Hello, Function 作为响应,因为query["name"] 将具有“函数”的值。

为了返回Hello, World,您必须提供一个类似这样的网址- http://localhost:7071/api/HttpExample?name=World

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多