Skip to content

Commit

Permalink
feat: use endpoint routing
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanho committed Sep 13, 2023
1 parent 4e23072 commit c4d6e98
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 17 deletions.
19 changes: 17 additions & 2 deletions src/Postal.AspNetCore/Email.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
using System.Dynamic;
using System.Net.Mail;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
using Postal.AspNetCore;

namespace Postal
Expand Down Expand Up @@ -131,7 +132,21 @@ string DeriveViewNameFromClassName()
return viewName;
}

public IList<IRouter> Routers { get; set; }
public RequestPath RequestPath { get; set; }
internal HttpContextData HttpContextData { get; private set; }

public void CaptureHttpContext(HttpContext httpContext)
{
var endpoint = httpContext.GetEndpoint();
var routeValues = httpContext.Features.Get<IRouteValuesFeature>()?.RouteValues;
HttpContextData = new HttpContextData { Endpoint = endpoint, RouteValues = routeValues };

RequestPath = new RequestPath();
RequestPath.PathBase = httpContext.Request.PathBase.ToString();
RequestPath.Host = httpContext.Request.Host.ToString();
RequestPath.IsHttps = httpContext.Request.IsHttps;
RequestPath.Scheme = httpContext.Request.Scheme;
RequestPath.Method = httpContext.Request.Method;
}
}
}
7 changes: 6 additions & 1 deletion src/Postal.AspNetCore/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ public static EmailService Create(IServiceProvider serviceProvider, Func<SmtpCli
/// <summary>
/// Creates a new <see cref="EmailService"/>.
/// </summary>
public EmailService(IEmailViewRender emailViewRenderer, IEmailParser emailParser, IOptions<EmailServiceOptions> options, ILogger<EmailService> logger)
public EmailService(
IEmailViewRender emailViewRenderer,
IEmailParser emailParser,
IOptions<EmailServiceOptions> options,
ILogger<EmailService> logger
)
{
this.emailViewRenderer = emailViewRenderer;
this.emailParser = emailParser;
Expand Down
11 changes: 1 addition & 10 deletions src/Postal.AspNetCore/EmailViewRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,8 @@ public virtual Task<string> RenderAsync(Email email)
public virtual async Task<string> RenderAsync(Email email, string viewName = null)
{
viewName = viewName ?? email.ViewName;
//var controllerContext = CreateControllerContext(email.AreaName, url);
//var view = CreateView(viewName, controllerContext);

var routeData = new Microsoft.AspNetCore.Routing.RouteData();
if (email.Routers != null && email.Routers.Count > 0)
{
foreach (var r in email.Routers)
{
routeData.Routers.Add(r);
}
}
routeData.Values["controller"] = EmailViewDirectoryName;
routeData.Values["page"] = EmailViewDirectoryName;
if (!string.IsNullOrWhiteSpace(email.AreaName))
Expand All @@ -71,7 +62,7 @@ public virtual async Task<string> RenderAsync(Email email, string viewName = nul

Dictionary<string, object> viewData = new Dictionary<string, object>();
viewData[ImageEmbedder.ViewDataKey] = email.ImageEmbedder;
var viewOutput = await _templateService.RenderTemplateAsync(routeData, viewName, email, viewData, true);
var viewOutput = await _templateService.RenderTemplateAsync(email.HttpContextData, routeData, viewName, email, viewData, true);
viewData.Remove(ImageEmbedder.ViewDataKey);
return viewOutput;
}
Expand Down
16 changes: 16 additions & 0 deletions src/Postal.AspNetCore/HttpContextData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Postal.AspNetCore
{
public class HttpContextData
{
public Endpoint Endpoint { get; internal set; }
public RouteValueDictionary RouteValues { get; internal set; }
}
}
20 changes: 20 additions & 0 deletions src/Postal.AspNetCore/InternalClass/EndpointFeature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Postal.AspNetCore.InternalClass
{
internal sealed class EndpointFeature : IEndpointFeature
{
public Endpoint Endpoint { get; set; }

public EndpointFeature(Endpoint endpoint)
{
Endpoint = endpoint;
}
}
}
2 changes: 1 addition & 1 deletion src/Postal.AspNetCore/TemplateServices/ITemplateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface ITemplateService
/// <param name="filename">Filename of the template to render</param>
/// <param name="viewModel">View model to use for rendering the template</param>
/// <returns>Returns the rendered template content</returns>
Task<string> RenderTemplateAsync<TViewModel>(RouteData routeData, string viewName, TViewModel viewModel,
Task<string> RenderTemplateAsync<TViewModel>(HttpContextData HttpContextData, RouteData routeData, string viewName, TViewModel viewModel,
Dictionary<string, object> additonalViewDictionary = null, bool isMainPage = true) where TViewModel : IViewData;
}
}
14 changes: 11 additions & 3 deletions src/Postal.AspNetCore/TemplateServices/TemplateService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Razor;
Expand All @@ -7,6 +9,7 @@
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Postal.AspNetCore.InternalClass;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -50,13 +53,16 @@ DiagnosticListener diagnosticListener
_diagnosticListener = diagnosticListener;
}

public async Task<string> RenderTemplateAsync<TViewModel>(RouteData routeData,
public async Task<string> RenderTemplateAsync<TViewModel>(HttpContextData httpContextData, RouteData routeData,
string viewName, TViewModel viewModel, Dictionary<string, object> additonalViewDictionary = null, bool isMainPage = true) where TViewModel : IViewData
{
var httpContext = new DefaultHttpContext
var httpContext = new DefaultHttpContext()
{
RequestServices = _serviceProvider
RequestServices = _serviceProvider,
};
httpContext.Features.Set<IEndpointFeature>(new EndpointFeature(httpContextData.Endpoint));
httpContext.Features.Set<IRouteValuesFeature>(new RouteValuesFeature() { RouteValues = httpContextData.RouteValues });

if (viewModel.RequestPath != null)
{
httpContext.Request.Host = HostString.FromUriComponent(viewModel.RequestPath.Host);
Expand Down Expand Up @@ -135,13 +141,15 @@ DiagnosticListener diagnosticListener
{
if (viewResult.Success)
{
_logger.LogDebug($"View template found: {viewResult.View.Path}");
var viewContext = new ViewContext(actionContext, viewResult.View, viewDictionary,
tempDataDictionary, outputWriter, new HtmlHelperOptions());

await viewResult.View.RenderAsync(viewContext);
}
else if (razorPageResult?.Page != null)
{
_logger.LogDebug($"Razor page found: {razorPageResult?.Page.Path}");
var page = razorPageResult?.Page;
var razorView = new RazorView(
_viewEngine,
Expand Down

0 comments on commit c4d6e98

Please sign in to comment.