-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleFunction.cs
More file actions
43 lines (36 loc) · 1.43 KB
/
SampleFunction.cs
File metadata and controls
43 lines (36 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace Microsoft.Azure.Functions.Samples.DependencyInjectionBasic
{
public class SampleFunction
{
private readonly IGreeter _greeter;
public SampleFunction(IGreeter greeter)
{
_greeter = greeter;
}
[FunctionName("SampleFunction")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "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(_greeter.CreateGreeting(name))
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
}