- 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.
推薦閱讀
- HTML5+CSS3+JavaScript從入門到精通:上冊(微課精編版·第2版)
- Advanced Quantitative Finance with C++
- Facebook Application Development with Graph API Cookbook
- 神經(jīng)網(wǎng)絡(luò)編程實戰(zhàn):Java語言實現(xiàn)(原書第2版)
- 深入淺出DPDK
- 深入分布式緩存:從原理到實踐
- R數(shù)據(jù)科學(xué)實戰(zhàn):工具詳解與案例分析
- Spring Boot實戰(zhàn)
- OpenStack Networking Essentials
- Python 3 數(shù)據(jù)分析與機器學(xué)習(xí)實戰(zhàn)
- RESTful Web Clients:基于超媒體的可復(fù)用客戶端
- C陷阱與缺陷
- Java Web應(yīng)用開發(fā)給力起飛
- Arduino電子設(shè)計實戰(zhàn)指南:零基礎(chǔ)篇
- PHP動態(tài)網(wǎng)站開發(fā)實踐教程