-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b543e3
commit 1380732
Showing
26 changed files
with
564 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Mvc.Razor; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.AspNetCore.Mvc.ViewFeatures; | ||
using Microsoft.AspNetCore.Mvc.ViewFeatures.Infrastructure; | ||
using RenderRazor.Views.Email; | ||
using Resend; | ||
using System.Text; | ||
|
||
namespace RenderRazor.Controllers; | ||
|
||
/// <summary /> | ||
[ApiController] | ||
public class EmailController : ControllerBase | ||
{ | ||
private readonly ILogger<EmailController> _logger; | ||
private readonly IRazorViewEngine _razor; | ||
private readonly IServiceProvider _services; | ||
private readonly IResend _resend; | ||
|
||
|
||
/// <summary /> | ||
public EmailController( IRazorViewEngine razor, IServiceProvider services, IResend resend, ILogger<EmailController> logger ) | ||
{ | ||
_razor = razor; | ||
_services = services; | ||
_resend = resend; | ||
_logger = logger; | ||
} | ||
|
||
|
||
/// <summary /> | ||
[HttpGet] | ||
[Route( "email/render" )] | ||
public async Task<string> EmailRender() | ||
{ | ||
var html = await RenderEmail( "Hello", new HelloModel() | ||
{ | ||
DisplayName = "John Doe", | ||
Items = { "NodeJs", "Go", "and many more..." }, | ||
SenderName = "Resend Team", | ||
} ); | ||
|
||
return html; | ||
} | ||
|
||
|
||
/// <summary /> | ||
[HttpGet] | ||
[Route( "email/send" )] | ||
public async Task<string> EmailSend() | ||
{ | ||
/* | ||
* | ||
*/ | ||
var html = await RenderEmail( "Hello", new HelloModel() | ||
{ | ||
DisplayName = "John Doe", | ||
Items = { "NodeJs", "Go", "and many more..." }, | ||
SenderName = "Resend Team", | ||
} ); | ||
|
||
|
||
/* | ||
* | ||
*/ | ||
var message = new EmailMessage(); | ||
message.From = "you@domain.com"; | ||
message.To.Add( "user@gmail.com" ); | ||
message.Subject = "Hello from Render Razor"; | ||
message.HtmlBody = html; | ||
|
||
var resp = await _resend.EmailSendAsync( message ); | ||
|
||
_logger.LogInformation( "Sent email, with Id = {EmailId}", resp.Content ); | ||
|
||
return resp.Content.ToString(); | ||
} | ||
|
||
|
||
|
||
/// <summary /> | ||
public async Task<string> RenderEmail<T>( string view, T model ) | ||
{ | ||
/* | ||
* | ||
*/ | ||
var viewEngineResult = _razor.GetView( "~/Views/Email/", $"{view}.cshtml", false ); | ||
|
||
if ( viewEngineResult == null ) | ||
throw new ApplicationException( "View not found" ); | ||
|
||
if ( viewEngineResult.Success == false ) | ||
throw new ApplicationException( "View failed" ); | ||
|
||
|
||
/* | ||
* | ||
*/ | ||
var actionContext = GetActionContext(); | ||
|
||
|
||
/* | ||
* | ||
*/ | ||
var sb = new StringBuilder(); | ||
|
||
using ( var sw = new StringWriter( sb ) ) | ||
{ | ||
var tempDataSerializer = (TempDataSerializer) _services.GetService( typeof( TempDataSerializer ) )!; | ||
var tempDataProvider = new SessionStateTempDataProvider( tempDataSerializer ); | ||
|
||
var viewContext = new ViewContext( | ||
actionContext, | ||
viewEngineResult.View, | ||
new ViewDataDictionary<T>( | ||
metadataProvider: new EmptyModelMetadataProvider(), | ||
modelState: new ModelStateDictionary() | ||
) | ||
{ | ||
Model = model | ||
}, | ||
new TempDataDictionary( actionContext.HttpContext, tempDataProvider ), | ||
sw, | ||
new HtmlHelperOptions() | ||
); | ||
|
||
await viewEngineResult.View.RenderAsync( viewContext ); | ||
} | ||
|
||
|
||
return sb.ToString(); | ||
} | ||
|
||
|
||
/// <summary /> | ||
private ActionContext GetActionContext() | ||
{ | ||
var httpContext = new DefaultHttpContext | ||
{ | ||
RequestServices = _services | ||
}; | ||
|
||
return new ActionContext( httpContext, new RouteData(), new ActionDescriptor() ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@page | ||
@model ErrorModel | ||
@{ | ||
ViewData["Title"] = "Error"; | ||
} | ||
|
||
<h1 class="text-danger">Error.</h1> | ||
<h2 class="text-danger">An error occurred while processing your request.</h2> | ||
|
||
@if (Model.ShowRequestId) | ||
{ | ||
<p> | ||
<strong>Request ID:</strong> <code>@Model.RequestId</code> | ||
</p> | ||
} | ||
|
||
<h3>Development Mode</h3> | ||
<p> | ||
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred. | ||
</p> | ||
<p> | ||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> | ||
It can result in displaying sensitive information from exceptions to end users. | ||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> | ||
and restarting the app. | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using System.Diagnostics; | ||
|
||
namespace RenderRazor.Pages; | ||
|
||
/// <summary /> | ||
[ResponseCache( Duration = 0, Location = ResponseCacheLocation.None, NoStore = true )] | ||
[IgnoreAntiforgeryToken] | ||
public class ErrorModel : PageModel | ||
{ | ||
/// <summary /> | ||
public string? RequestId { get; set; } | ||
|
||
/// <summary /> | ||
public bool ShowRequestId => !string.IsNullOrEmpty( RequestId ); | ||
|
||
private readonly ILogger<ErrorModel> _logger; | ||
|
||
|
||
/// <summary /> | ||
public ErrorModel( ILogger<ErrorModel> logger ) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
|
||
/// <summary /> | ||
public void OnGet() | ||
{ | ||
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@page | ||
@model IndexModel | ||
@{ | ||
ViewData["Title"] = "Home page"; | ||
} | ||
|
||
<div> | ||
<img class="x-logo" src="https://mintlify.s3.us-west-1.amazonaws.com/resend/logo-white.svg" alt="Resend" /> | ||
</div> | ||
|
||
<div class="x-description"> | ||
Example website, which shows how a controller can generate an HTML body | ||
by using a Razor view. | ||
</div> | ||
|
||
<div class="x-links"> | ||
<ul> | ||
<li> | ||
<a href="/email/render">Render HTML</a> | ||
</li> | ||
|
||
<li> | ||
<a href="/email/send">Send rendered HTML</a> | ||
</li> | ||
</ul> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace RenderRazor.Pages; | ||
|
||
/// <summary /> | ||
public class IndexModel : PageModel | ||
{ | ||
private readonly ILogger<IndexModel> _logger; | ||
|
||
|
||
/// <summary /> | ||
public IndexModel( ILogger<IndexModel> logger ) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
|
||
/// <summary /> | ||
public void OnGet() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>@ViewData["Title"] - Render Razor</title> | ||
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" /> | ||
</head> | ||
<body> | ||
<main role="main"> | ||
@RenderBody() | ||
</main> | ||
|
||
<script src="~/js/site.js" asp-append-version="true"></script> | ||
|
||
@await RenderSectionAsync("Scripts", required: false) | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* Please see documentation at https://learn.microsoft.com/aspnet/core/client-side/bundling-and-minification | ||
for details on configuring this project to bundle and minify static web assets. */ | ||
|
||
a.navbar-brand { | ||
white-space: normal; | ||
text-align: center; | ||
word-break: break-all; | ||
} | ||
|
||
a { | ||
color: #0077cc; | ||
} | ||
|
||
.btn-primary { | ||
color: #fff; | ||
background-color: #1b6ec2; | ||
border-color: #1861ac; | ||
} | ||
|
||
.nav-pills .nav-link.active, .nav-pills .show > .nav-link { | ||
color: #fff; | ||
background-color: #1b6ec2; | ||
border-color: #1861ac; | ||
} | ||
|
||
.border-top { | ||
border-top: 1px solid #e5e5e5; | ||
} | ||
.border-bottom { | ||
border-bottom: 1px solid #e5e5e5; | ||
} | ||
|
||
.box-shadow { | ||
box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05); | ||
} | ||
|
||
button.accept-policy { | ||
font-size: 1rem; | ||
line-height: inherit; | ||
} | ||
|
||
.footer { | ||
position: absolute; | ||
bottom: 0; | ||
width: 100%; | ||
white-space: nowrap; | ||
line-height: 60px; | ||
} |
1 change: 1 addition & 0 deletions
1
examples/RenderRazor/Pages/Shared/_ValidationScriptsPartial.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@using RenderRazor | ||
@namespace RenderRazor.Pages | ||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@{ | ||
Layout = "_Layout"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Resend; | ||
|
||
namespace RenderRazor; | ||
|
||
/// <summary /> | ||
public class Program | ||
{ | ||
/// <summary /> | ||
public static void Main( string[] args ) | ||
{ | ||
/* | ||
* | ||
*/ | ||
var builder = WebApplication.CreateBuilder( args ); | ||
|
||
builder.Services.AddControllers(); | ||
builder.Services.AddRazorPages(); | ||
|
||
// Resend | ||
builder.Services.AddOptions(); | ||
builder.Services.Configure<ResendClientOptions>( o => | ||
{ | ||
o.ApiToken = Environment.GetEnvironmentVariable( "RESEND_APITOKEN" )!; | ||
} ); | ||
builder.Services.AddHttpClient<ResendClient>(); | ||
builder.Services.AddTransient<IResend, ResendClient>(); | ||
|
||
|
||
/* | ||
* | ||
*/ | ||
var app = builder.Build(); | ||
|
||
app.UseStaticFiles(); | ||
app.MapRazorPages(); | ||
app.MapControllers(); | ||
|
||
|
||
/* | ||
* | ||
*/ | ||
app.Run(); | ||
} | ||
} |
Oops, something went wrong.