├── .gitignore
├── .vscode
├── extensions.json
├── launch.json
├── settings.json
└── tasks.json
├── AzureFunctionsRazorEmailTemplateSample.csproj
├── AzureFunctionsRazorEmailTemplateSample.sln
├── LICENSE
├── RazorViewToStringRenderer.cs
├── SendRazorTemplateEmail.cs
├── Startup.cs
├── Views
├── SampleEmailTemplate.cshtml
└── SampleEmailTemplate.cshtml.cs
└── host.json
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # Azure Functions localsettings file
5 | local.settings.json
6 |
7 | # User-specific files
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Build results
17 | [Dd]ebug/
18 | [Dd]ebugPublic/
19 | [Rr]elease/
20 | [Rr]eleases/
21 | x64/
22 | x86/
23 | bld/
24 | [Bb]in/
25 | [Oo]bj/
26 | [Ll]og/
27 |
28 | # Visual Studio 2015 cache/options directory
29 | .vs/
30 | # Uncomment if you have tasks that create the project's static files in wwwroot
31 | #wwwroot/
32 |
33 | # MSTest test Results
34 | [Tt]est[Rr]esult*/
35 | [Bb]uild[Ll]og.*
36 |
37 | # NUNIT
38 | *.VisualState.xml
39 | TestResult.xml
40 |
41 | # Build Results of an ATL Project
42 | [Dd]ebugPS/
43 | [Rr]eleasePS/
44 | dlldata.c
45 |
46 | # DNX
47 | project.lock.json
48 | project.fragment.lock.json
49 | artifacts/
50 |
51 | *_i.c
52 | *_p.c
53 | *_i.h
54 | *.ilk
55 | *.meta
56 | *.obj
57 | *.pch
58 | *.pdb
59 | *.pgc
60 | *.pgd
61 | *.rsp
62 | *.sbr
63 | *.tlb
64 | *.tli
65 | *.tlh
66 | *.tmp
67 | *.tmp_proj
68 | *.log
69 | *.vspscc
70 | *.vssscc
71 | .builds
72 | *.pidb
73 | *.svclog
74 | *.scc
75 |
76 | # Chutzpah Test files
77 | _Chutzpah*
78 |
79 | # Visual C++ cache files
80 | ipch/
81 | *.aps
82 | *.ncb
83 | *.opendb
84 | *.opensdf
85 | *.sdf
86 | *.cachefile
87 | *.VC.db
88 | *.VC.VC.opendb
89 |
90 | # Visual Studio profiler
91 | *.psess
92 | *.vsp
93 | *.vspx
94 | *.sap
95 |
96 | # TFS 2012 Local Workspace
97 | $tf/
98 |
99 | # Guidance Automation Toolkit
100 | *.gpState
101 |
102 | # ReSharper is a .NET coding add-in
103 | _ReSharper*/
104 | *.[Rr]e[Ss]harper
105 | *.DotSettings.user
106 |
107 | # JustCode is a .NET coding add-in
108 | .JustCode
109 |
110 | # TeamCity is a build add-in
111 | _TeamCity*
112 |
113 | # DotCover is a Code Coverage Tool
114 | *.dotCover
115 |
116 | # NCrunch
117 | _NCrunch_*
118 | .*crunch*.local.xml
119 | nCrunchTemp_*
120 |
121 | # MightyMoose
122 | *.mm.*
123 | AutoTest.Net/
124 |
125 | # Web workbench (sass)
126 | .sass-cache/
127 |
128 | # Installshield output folder
129 | [Ee]xpress/
130 |
131 | # DocProject is a documentation generator add-in
132 | DocProject/buildhelp/
133 | DocProject/Help/*.HxT
134 | DocProject/Help/*.HxC
135 | DocProject/Help/*.hhc
136 | DocProject/Help/*.hhk
137 | DocProject/Help/*.hhp
138 | DocProject/Help/Html2
139 | DocProject/Help/html
140 |
141 | # Click-Once directory
142 | publish/
143 |
144 | # Publish Web Output
145 | *.[Pp]ublish.xml
146 | *.azurePubxml
147 | # TODO: Comment the next line if you want to checkin your web deploy settings
148 | # but database connection strings (with potential passwords) will be unencrypted
149 | #*.pubxml
150 | *.publishproj
151 |
152 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
153 | # checkin your Azure Web App publish settings, but sensitive information contained
154 | # in these scripts will be unencrypted
155 | PublishScripts/
156 |
157 | # NuGet Packages
158 | *.nupkg
159 | # The packages folder can be ignored because of Package Restore
160 | **/packages/*
161 | # except build/, which is used as an MSBuild target.
162 | !**/packages/build/
163 | # Uncomment if necessary however generally it will be regenerated when needed
164 | #!**/packages/repositories.config
165 | # NuGet v3's project.json files produces more ignoreable files
166 | *.nuget.props
167 | *.nuget.targets
168 |
169 | # Microsoft Azure Build Output
170 | csx/
171 | *.build.csdef
172 |
173 | # Microsoft Azure Emulator
174 | ecf/
175 | rcf/
176 |
177 | # Windows Store app package directories and files
178 | AppPackages/
179 | BundleArtifacts/
180 | Package.StoreAssociation.xml
181 | _pkginfo.txt
182 |
183 | # Visual Studio cache files
184 | # files ending in .cache can be ignored
185 | *.[Cc]ache
186 | # but keep track of directories ending in .cache
187 | !*.[Cc]ache/
188 |
189 | # Others
190 | ClientBin/
191 | ~$*
192 | *~
193 | *.dbmdl
194 | *.dbproj.schemaview
195 | *.jfm
196 | *.pfx
197 | *.publishsettings
198 | node_modules/
199 | orleans.codegen.cs
200 |
201 | # Since there are multiple workflows, uncomment next line to ignore bower_components
202 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
203 | #bower_components/
204 |
205 | # RIA/Silverlight projects
206 | Generated_Code/
207 |
208 | # Backup & report files from converting an old project file
209 | # to a newer Visual Studio version. Backup files are not needed,
210 | # because we have git ;-)
211 | _UpgradeReport_Files/
212 | Backup*/
213 | UpgradeLog*.XML
214 | UpgradeLog*.htm
215 |
216 | # SQL Server files
217 | *.mdf
218 | *.ldf
219 |
220 | # Business Intelligence projects
221 | *.rdl.data
222 | *.bim.layout
223 | *.bim_*.settings
224 |
225 | # Microsoft Fakes
226 | FakesAssemblies/
227 |
228 | # GhostDoc plugin setting file
229 | *.GhostDoc.xml
230 |
231 | # Node.js Tools for Visual Studio
232 | .ntvs_analysis.dat
233 |
234 | # Visual Studio 6 build log
235 | *.plg
236 |
237 | # Visual Studio 6 workspace options file
238 | *.opt
239 |
240 | # Visual Studio LightSwitch build output
241 | **/*.HTMLClient/GeneratedArtifacts
242 | **/*.DesktopClient/GeneratedArtifacts
243 | **/*.DesktopClient/ModelManifest.xml
244 | **/*.Server/GeneratedArtifacts
245 | **/*.Server/ModelManifest.xml
246 | _Pvt_Extensions
247 |
248 | # Paket dependency manager
249 | .paket/paket.exe
250 | paket-files/
251 |
252 | # FAKE - F# Make
253 | .fake/
254 |
255 | # JetBrains Rider
256 | .idea/
257 | *.sln.iml
258 |
259 | # CodeRush
260 | .cr/
261 |
262 | # Python Tools for Visual Studio (PTVS)
263 | __pycache__/
264 | *.pyc
--------------------------------------------------------------------------------
/.vscode/extensions.json:
--------------------------------------------------------------------------------
1 | {
2 | "recommendations": [
3 | "ms-azuretools.vscode-azurefunctions",
4 | "ms-dotnettools.csharp"
5 | ]
6 | }
7 |
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "0.2.0",
3 | "configurations": [
4 | {
5 | "name": "Attach to .NET Functions",
6 | "type": "coreclr",
7 | "request": "attach",
8 | "processId": "${command:azureFunctions.pickProcess}"
9 | }
10 | ]
11 | }
12 |
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "azureFunctions.deploySubpath": "bin/Release/netcoreapp3.1/publish",
3 | "azureFunctions.projectLanguage": "C#",
4 | "azureFunctions.projectRuntime": "~3",
5 | "debug.internalConsoleOptions": "neverOpen",
6 | "azureFunctions.preDeployTask": "publish"
7 | }
8 |
--------------------------------------------------------------------------------
/.vscode/tasks.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "2.0.0",
3 | "tasks": [
4 | {
5 | "label": "clean",
6 | "command": "dotnet",
7 | "args": [
8 | "clean",
9 | "/property:GenerateFullPaths=true",
10 | "/consoleloggerparameters:NoSummary"
11 | ],
12 | "type": "process",
13 | "problemMatcher": "$msCompile"
14 | },
15 | {
16 | "label": "build",
17 | "command": "dotnet",
18 | "args": [
19 | "build",
20 | "/property:GenerateFullPaths=true",
21 | "/consoleloggerparameters:NoSummary"
22 | ],
23 | "type": "process",
24 | "dependsOn": "clean",
25 | "group": {
26 | "kind": "build",
27 | "isDefault": true
28 | },
29 | "problemMatcher": "$msCompile"
30 | },
31 | {
32 | "label": "clean release",
33 | "command": "dotnet",
34 | "args": [
35 | "clean",
36 | "--configuration",
37 | "Release",
38 | "/property:GenerateFullPaths=true",
39 | "/consoleloggerparameters:NoSummary"
40 | ],
41 | "type": "process",
42 | "problemMatcher": "$msCompile"
43 | },
44 | {
45 | "label": "publish",
46 | "command": "dotnet",
47 | "args": [
48 | "publish",
49 | "--configuration",
50 | "Release",
51 | "/property:GenerateFullPaths=true",
52 | "/consoleloggerparameters:NoSummary"
53 | ],
54 | "type": "process",
55 | "dependsOn": "clean release",
56 | "problemMatcher": "$msCompile"
57 | },
58 | {
59 | "type": "func",
60 | "dependsOn": "build",
61 | "options": {
62 | "cwd": "${workspaceFolder}/bin/Debug/netcoreapp3.1"
63 | },
64 | "command": "host start",
65 | "isBackground": true,
66 | "problemMatcher": "$func-watch"
67 | }
68 | ]
69 | }
70 |
--------------------------------------------------------------------------------
/AzureFunctionsRazorEmailTemplateSample.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | netcoreapp3.1
4 | v3
5 | True
6 | True
7 | True
8 | True
9 | True
10 | False
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | PreserveNewest
25 |
26 |
27 | PreserveNewest
28 | Never
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/AzureFunctionsRazorEmailTemplateSample.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.30114.105
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AzureFunctionsRazorEmailTemplateSample", "AzureFunctionsRazorEmailTemplateSample.csproj", "{A976D161-88C5-4828-B01C-B695DEB72637}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {A976D161-88C5-4828-B01C-B695DEB72637}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {A976D161-88C5-4828-B01C-B695DEB72637}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {A976D161-88C5-4828-B01C-B695DEB72637}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {A976D161-88C5-4828-B01C-B695DEB72637}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {29E71494-AAF7-495B-AA4C-EC4D05DD8266}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Etienne Peeters
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/RazorViewToStringRenderer.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Http;
2 | using Microsoft.AspNetCore.Mvc;
3 | using Microsoft.AspNetCore.Mvc.Abstractions;
4 | using Microsoft.AspNetCore.Mvc.ModelBinding;
5 | using Microsoft.AspNetCore.Mvc.Razor;
6 | using Microsoft.AspNetCore.Mvc.Rendering;
7 | using Microsoft.AspNetCore.Mvc.ViewEngines;
8 | using Microsoft.AspNetCore.Mvc.ViewFeatures;
9 | using Microsoft.AspNetCore.Routing;
10 | using System;
11 | using System.IO;
12 | using System.Linq;
13 | using System.Threading.Tasks;
14 |
15 | namespace AzureFunctionsRazorEmailTemplateSample
16 | {
17 | public class RazorViewToStringRenderer
18 | {
19 | private readonly IRazorViewEngine viewEngine;
20 | private readonly ITempDataProvider tempDataProvider;
21 | private readonly IServiceProvider serviceProvider;
22 |
23 | public RazorViewToStringRenderer(IRazorViewEngine viewEngine, ITempDataProvider tempDataProvider, IServiceProvider serviceProvider)
24 | {
25 | this.viewEngine = viewEngine;
26 | this.tempDataProvider = tempDataProvider;
27 | this.serviceProvider = serviceProvider;
28 | }
29 |
30 | ///
31 | /// Renders a Razor view and model to a string.
32 | /// The name of the view is automaticaly inferred from the model name (e.g. IndexModel -> Index).
33 | ///
34 | ///
35 | ///
36 | ///
37 | public async Task RenderViewToStringAsync(TModel model)
38 | {
39 | var templateName = typeof(TModel).Name.Replace("Model", "");
40 | return await RenderViewToStringAsync(model, templateName);
41 | }
42 |
43 | ///
44 | /// Renders a Razor view and model to a string.
45 | ///
46 | ///
47 | ///
48 | ///
49 | public async Task RenderViewToStringAsync(TModel model, string viewName)
50 | {
51 | var actionContext = GetActionContext();
52 | var view = FindView(actionContext, viewName);
53 |
54 | using var output = new StringWriter();
55 | var viewDataDictionary = new ViewDataDictionary(
56 | metadataProvider: new EmptyModelMetadataProvider(), modelState: new ModelStateDictionary())
57 | {
58 | Model = model
59 | };
60 | var tempDataDictionary = new TempDataDictionary(actionContext.HttpContext, tempDataProvider);
61 | var viewContext = new ViewContext(actionContext, view, viewDataDictionary, tempDataDictionary, output, new HtmlHelperOptions());
62 |
63 | await view.RenderAsync(viewContext);
64 |
65 | return output.ToString();
66 | }
67 |
68 | private IView FindView(ActionContext actionContext, string viewName)
69 | {
70 | var getViewResult = viewEngine.GetView(executingFilePath: null, viewPath: viewName, isMainPage: true);
71 | if (getViewResult.Success) return getViewResult.View;
72 |
73 | var findViewResult = viewEngine.FindView(actionContext, viewName, isMainPage: true);
74 | if (findViewResult.Success) return findViewResult.View;
75 |
76 | var searchedLocations = getViewResult.SearchedLocations.Concat(findViewResult.SearchedLocations);
77 | var errorMessage = string.Join(Environment.NewLine,
78 | new[] { $"Unable to find view '{viewName}'. The following locations were searched:" }.Concat(searchedLocations));
79 | throw new InvalidOperationException(errorMessage);
80 | }
81 |
82 | private ActionContext GetActionContext()
83 | {
84 | var httpContext = new DefaultHttpContext { RequestServices = serviceProvider };
85 | return new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/SendRazorTemplateEmail.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using System.Threading.Tasks;
3 | using Microsoft.AspNetCore.Mvc;
4 | using Microsoft.Azure.WebJobs;
5 | using Microsoft.Azure.WebJobs.Extensions.Http;
6 | using Microsoft.AspNetCore.Http;
7 | using Microsoft.Extensions.Logging;
8 | using Newtonsoft.Json;
9 | using SendGrid.Helpers.Mail;
10 | using SendGrid;
11 | using System.Threading;
12 | using AzureFunctionsRazorEmailTemplateSample.Views;
13 |
14 | namespace AzureFunctionsRazorEmailTemplateSample.Function
15 | {
16 | public class SendRazorTemplateEmail
17 | {
18 | private readonly RazorViewToStringRenderer razorViewToStringRenderer;
19 |
20 | public SendRazorTemplateEmail(RazorViewToStringRenderer razorViewToStringRenderer)
21 | {
22 | this.razorViewToStringRenderer = razorViewToStringRenderer;
23 | }
24 |
25 | [FunctionName("SendRazorTemplateEmail")]
26 | public async Task Run(
27 | [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
28 | [SendGrid] IAsyncCollector messageCollector,
29 | ILogger log, CancellationToken cancellationToken)
30 | {
31 | string name = req.Query["name"];
32 |
33 | string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
34 | dynamic data = JsonConvert.DeserializeObject(requestBody);
35 | name = name ?? data?.name;
36 |
37 | var message = new SendGridMessage();
38 |
39 | message.SetFrom("from@example.com");
40 | message.AddTo("to@example.com");
41 | message.SetSubject("Subject");
42 |
43 | var model = new SampleEmailTemplateModel()
44 | {
45 | Name = name
46 | };
47 | var htmlContent = await razorViewToStringRenderer.RenderViewToStringAsync(model);
48 | message.AddContent(MimeType.Html, htmlContent);
49 |
50 | await messageCollector.AddAsync(message, cancellationToken);
51 |
52 | log.LogInformation("C# HTTP trigger function processed a request.");
53 | return new OkObjectResult(htmlContent);
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/Startup.cs:
--------------------------------------------------------------------------------
1 | using AzureFunctionsRazorEmailTemplateSample;
2 | using Microsoft.Azure.Functions.Extensions.DependencyInjection;
3 | using Microsoft.Extensions.DependencyInjection;
4 | using Microsoft.Extensions.Localization;
5 | using Microsoft.Extensions.ObjectPool;
6 | using System.IO;
7 | using System.Reflection;
8 |
9 | [assembly: FunctionsStartup(typeof(Startup))]
10 | namespace AzureFunctionsRazorEmailTemplateSample
11 | {
12 | class Startup : FunctionsStartup
13 | {
14 | public override void Configure(IFunctionsHostBuilder builder)
15 | {
16 | string executionPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
17 | var compiledViewAssembly = Assembly.LoadFile(Path.Combine(executionPath, "AzureFunctionsRazorEmailTemplateSample.Views.dll"));
18 | builder.Services
19 | .AddSingleton()
20 | .AddScoped()
21 | .AddSingleton()
22 | .AddMvcCore()
23 | .AddViews()
24 | .AddRazorViewEngine()
25 | .AddApplicationPart(compiledViewAssembly);
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Views/SampleEmailTemplate.cshtml:
--------------------------------------------------------------------------------
1 | @model AzureFunctionsRazorEmailTemplateSample.Views.SampleEmailTemplateModel
2 |
3 |
4 |
5 |
6 |
7 |
8 | Hi @Model.Name, check out this samle template!
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Views/SampleEmailTemplate.cshtml.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc.RazorPages;
2 |
3 | namespace AzureFunctionsRazorEmailTemplateSample.Views
4 | {
5 | public class SampleEmailTemplateModel : PageModel
6 | {
7 | public string Name { get; set; }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/host.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "2.0",
3 | "logging": {
4 | "applicationInsights": {
5 | "samplingExcludedTypes": "Request",
6 | "samplingSettings": {
7 | "isEnabled": true
8 | }
9 | }
10 | }
11 | }
--------------------------------------------------------------------------------