|
1 |
| -# azure-functions-http-api |
2 |
| - |
| 1 | +# HTTP API Extensions for Azure Functions v3 |
| 2 | + |
| 3 | +[](https://dev.azure.com/shibayan/azure-functions-http-api/_build/latest?definitionId=57&branchName=master) |
| 4 | +[](https://github.com/shibayan/azure-functions-http-api/blob/master/LICENSE) |
| 5 | + |
| 6 | +## NuGet Packages |
| 7 | + |
| 8 | +Package Name | Target Framework | NuGet |
| 9 | +---|---|--- |
| 10 | +WebJobs.Extensions.HttpApi | .NET Core 3.1 | [](https://www.nuget.org/packages/WebJobs.Extensions.HttpApi) |
| 11 | + |
| 12 | +## Basic usage |
| 13 | + |
| 14 | +### Model validation |
| 15 | + |
| 16 | +```csharp |
| 17 | +public class Function1 : HttpFunctionBase |
| 18 | +{ |
| 19 | + public Function1(IHttpContextAccessor httpContextAccessor) |
| 20 | + : base(httpContextAccessor) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + [FunctionName("Function1")] |
| 25 | + public IActionResult Run( |
| 26 | + [HttpTrigger(AuthorizationLevel.Function, "post")] |
| 27 | + SampleModel model, |
| 28 | + ILogger log) |
| 29 | + { |
| 30 | + if (!TryValidateModel(model)) |
| 31 | + { |
| 32 | + return BadRequest(ModelState); |
| 33 | + } |
| 34 | + |
| 35 | + return Ok(model); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +public class SampleModel |
| 40 | +{ |
| 41 | + [Required] |
| 42 | + public string Name { get; set; } |
| 43 | + |
| 44 | + public string[] Array { get; set; } |
| 45 | + |
| 46 | + [Range(100, 10000)] |
| 47 | + public int Price { get; set; } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### ASP.NET Core like properties |
| 52 | + |
| 53 | +```csharp |
| 54 | +public class Function2 : HttpFunctionBase |
| 55 | +{ |
| 56 | + public Function2(IHttpContextAccessor httpContextAccessor) |
| 57 | + : base(httpContextAccessor) |
| 58 | + { |
| 59 | + } |
| 60 | + |
| 61 | + [FunctionName("Function2")] |
| 62 | + public IActionResult Run( |
| 63 | + [HttpTrigger(AuthorizationLevel.Function, "get")] |
| 64 | + HttpRequest req, |
| 65 | + ILogger log) |
| 66 | + { |
| 67 | + Response.Headers.Add("Cache-Control", "no-cache"); |
| 68 | + |
| 69 | + return Ok($"Now: {DateTime.Now}"); |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### Support URL generation |
| 75 | + |
| 76 | +```csharp |
| 77 | +public class Function3 : HttpFunctionBase |
| 78 | +{ |
| 79 | + public Function3(IHttpContextAccessor httpContextAccessor) |
| 80 | + : base(httpContextAccessor) |
| 81 | + { |
| 82 | + } |
| 83 | + |
| 84 | + [FunctionName("Function3")] |
| 85 | + public IActionResult Run( |
| 86 | + [HttpTrigger(AuthorizationLevel.Function, "get", Route = "route/{id}")] |
| 87 | + HttpRequest req, |
| 88 | + string id, |
| 89 | + ILogger log) |
| 90 | + { |
| 91 | + return CreatedAtFunction("Function3", new { id = "kazuakix" }, null); |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## License |
| 97 | + |
| 98 | +This project is licensed under the [MIT License](https://github.com/shibayan/azure-functions-http-api/blob/master/LICENSE) |
0 commit comments