Azure Functions 是 Microsoft Azure 云平台上的无服务器应用程序,无需担心运行它的基础结构。它与 AWS 云中的 lambda 函数非常相似。
下面的 Azure 函数返回一条消息,如下所示string
public static class BasicExample
{
[FunctionName("BasicExample")]
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;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
只需按开始调试 Azure 函数,它就会打开一个控制台,该控制台将提供用于访问浏览器的 URL。F5
Azure Functions Core Tools
Core Tools Version: 4.0.5198 Commit hash: N/A (64-bit)
Function Runtime Version: 4.21.1.20667
[2024-03-28T05:48:45.707Z] Found D:\Workspace\30DayChallenge.Net\AzureFunctionExample\AzureFunctionExample.csproj. Using for user secrets file configuration.
Functions:
BasicExample: [GET,POST] https://localhost:7073/api/BasicExample
在浏览器中打开 URL “”,开始运行函数终结点。https://localhost:7073/api/BasicExample
从浏览器返回的响应。
This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.
使用其他查询参数修改 URL,如下所示?name=Sukhpinder
https://localhost:7073/api/BasicExample?name=Sukhpinder
从浏览器返回的响应。
Hello, Sukhpinder. This HTTP triggered function executed successfully.
源代码获取:公众号回复消息【code:21108
】