- Azure Serverless Computing Cookbook
- Praveen Kumar Sreeram
- 436字
- 2021-07-02 20:23:00
How to do it…
- Navigate to the Function App listing page. Choose the function app in which you would like to add a new function.
- Create a new function by clicking on the + icon as shown in the following screenshot:

- If you have created a brand new function, then clicking on the + icon in the preceding step, you would see the Get started quickly with a premade function page. Please click on the create your own custom functions link to navigate to the page where you can see all the built-in templates for creating your Azure Functions.
- In the Choose a template below or go to the quickstart section, choose HTTPTrigger-CSharp as shown in the following screenshot to create a new HTTP trigger function:

- Provide a meaningful name. For this example, I have used RegisterUser as the name of the Azure Function.
- In the Authorization level drop-down, choose the Anonymous option as shown in the following screenshot. We will learn more about the all the authorization levels in Chapter 9, Implement Best Practices for Azure Functions:

- Once you provide the name and choose the Authorization level, click on Create button to create the HTTP trigger function.
- As soon as you create the function, all the required code and configuration files will be created automatically and the run.csx file will be opened for you to edit the code. Remove the default code and replace it with the following code:
using System.Net;
public static async Task<HttpResponseMessage>
Run(HttpRequestMessage req, TraceWriter log)
{
string firstname=null,lastname = null;
dynamic data = await req.Content.ReadAsAsync<object>();
firstname = firstname ?? data?.firstname;
lastname = data?.lastname;
return (lastname + firstname) == null ?
req.CreateResponse(HttpStatusCode.BadRequest,
"Please pass a name on the query string or in the
request body") :
req.CreateResponse(HttpStatusCode.OK, "Hello " +
firstname + " " + lastname);
}
- Save the changes by clicking on the Save button available just above the code editor.
- Let's try to test the RegisterUser function using the Test console. Click on the tab named Test as shown in the following screenshot to open the Test console:

- Enter the values for firstname and lastname, in the Request body section as shown in the following screenshot:

Please make sure you select POST in the HTTP method drop-down.
- Once you have reviewed the input parameters, click on the Run button available at the bottom of the Test console as shown in the following screenshot:

- If the input request workload is passed correctly with all the required parameters, you will see a Status 200 OK, and the output in the Output window will be as shown in the preceding screenshot.
推薦閱讀
- 一步一步學Spring Boot 2:微服務項目實戰
- Bootstrap Site Blueprints Volume II
- VSTO開發入門教程
- Drupal 8 Configuration Management
- C# 8.0核心技術指南(原書第8版)
- Java系統化項目開發教程
- Android嵌入式系統程序開發:基于Cortex-A8(第2版)
- Deep Learning with R Cookbook
- 進入IT企業必讀的324個Java面試題
- DB2SQL性能調優秘笈
- Java高手是怎樣煉成的:原理、方法與實踐
- Android技術內幕(系統卷)
- IBM RUP參考與認證指南
- 信息學奧林匹克競賽初賽精講精練
- 可視化H5頁面設計與制作:Mugeda標準教程