Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support views stored in Areas #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Postal/Email.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public Email(string viewName)
ImageEmbedder = new ImageEmbedder();
}

/// <summary>
/// Creates a new Email, that will render the given view.
/// </summary>
/// <param name="viewName">The name of the view to render</param>
/// <param name="areaName">The name of the area containing the view to render</param>
public Email(string viewName, string areaName)
: this(viewName)
{
AreaName = areaName;
}

/// <summary>Create an Email where the ViewName is derived from the name of the class.</summary>
/// <remarks>Used when defining strongly typed Email classes.</remarks>
protected Email()
Expand All @@ -45,6 +56,11 @@ protected Email()
/// </summary>
public string ViewName { get; set; }

/// <summary>
/// The name of the area containing the email template.
/// </summary>
public string AreaName { get; set; }

/// <summary>
/// The view data to pass to the view.
/// </summary>
Expand Down
14 changes: 12 additions & 2 deletions src/Postal/EmailViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ public EmailViewRenderer(ViewEngineCollection viewEngines)
public string Render(Email email, string viewName = null)
{
viewName = viewName ?? email.ViewName;
var controllerContext = CreateControllerContext();
var controllerContext = CreateControllerContext(email.AreaName);
var view = CreateView(viewName, controllerContext);
var viewOutput = RenderView(view, email.ViewData, controllerContext, email.ImageEmbedder);
return viewOutput;
}

ControllerContext CreateControllerContext()
/// <summary>
///
/// </summary>
/// <param name="areaName">The name of the area containing the Emails view folder if applicable</param>
/// <returns></returns>
ControllerContext CreateControllerContext(string areaName)
{
// A dummy HttpContextBase that is enough to allow the view to be rendered.
var httpContext = new HttpContextWrapper(
Expand All @@ -55,6 +60,11 @@ ControllerContext CreateControllerContext()
);
var routeData = new RouteData();
routeData.Values["controller"] = EmailViewDirectoryName;

// if populated will add searching the named Area for the view
if (!string.IsNullOrWhiteSpace(areaName))
routeData.DataTokens["Area"] = areaName;

var requestContext = new RequestContext(httpContext, routeData);
var stubController = new StubController();
var controllerContext = new ControllerContext(requestContext, stubController);
Expand Down