Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 93ee2cf

Browse files
committed
Use GetView in addition to FindView for view discovery
1 parent f8c8dbb commit 93ee2cf

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

samples/Mvc.RenderViewToString/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public static void Main()
2323
var emailContent = RenderViewAsync(serviceScopeFactory).Result;
2424

2525
Console.WriteLine(emailContent);
26-
Console.ReadLine();
2726
}
2827

2928
public static IServiceScopeFactory InitializeServices(string customApplicationBasePath = null)
@@ -53,7 +52,7 @@ public static Task<string> RenderViewAsync(IServiceScopeFactory scopeFactory)
5352
UserData2 = 2
5453
};
5554

56-
return helper.RenderViewToStringAsync("EmailTemplate", model);
55+
return helper.RenderViewToStringAsync("Views/EmailTemplate.cshtml", model);
5756
}
5857
}
5958

@@ -69,7 +68,7 @@ private static void ConfigureDefaultServices(IServiceCollection services, string
6968
else
7069
{
7170
applicationName = Assembly.GetEntryAssembly().GetName().Name;
72-
fileProvider = new PhysicalFileProvider(AppContext.BaseDirectory);
71+
fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
7372
}
7473

7574
services.AddSingleton<IHostingEnvironment>(new HostingEnvironment

samples/Mvc.RenderViewToString/RazorViewToStringRenderer.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
using System;
55
using System.IO;
6+
using System.Linq;
67
using System.Threading.Tasks;
78
using Microsoft.AspNetCore.Http;
89
using Microsoft.AspNetCore.Mvc;
910
using Microsoft.AspNetCore.Mvc.Abstractions;
1011
using Microsoft.AspNetCore.Mvc.ModelBinding;
1112
using Microsoft.AspNetCore.Mvc.Razor;
1213
using Microsoft.AspNetCore.Mvc.Rendering;
14+
using Microsoft.AspNetCore.Mvc.ViewEngines;
1315
using Microsoft.AspNetCore.Mvc.ViewFeatures;
1416
using Microsoft.AspNetCore.Routing;
1517

@@ -31,18 +33,10 @@ public RazorViewToStringRenderer(
3133
_serviceProvider = serviceProvider;
3234
}
3335

34-
public async Task<string> RenderViewToStringAsync<TModel>(string name, TModel model)
36+
public async Task<string> RenderViewToStringAsync<TModel>(string viewName, TModel model)
3537
{
3638
var actionContext = GetActionContext();
37-
38-
var viewEngineResult = _viewEngine.FindView(actionContext, name, false);
39-
40-
if (!viewEngineResult.Success)
41-
{
42-
throw new InvalidOperationException(string.Format("Couldn't find view '{0}'", name));
43-
}
44-
45-
var view = viewEngineResult.View;
39+
var view = FindView(actionContext, viewName);
4640

4741
using (var output = new StringWriter())
4842
{
@@ -67,6 +61,28 @@ public async Task<string> RenderViewToStringAsync<TModel>(string name, TModel mo
6761
}
6862
}
6963

64+
private IView FindView(ActionContext actionContext, string viewName)
65+
{
66+
var getViewResult = _viewEngine.GetView(executingFilePath: null, viewPath: viewName, isMainPage: true);
67+
if (getViewResult.Success)
68+
{
69+
return getViewResult.View;
70+
}
71+
72+
var findViewResult = _viewEngine.FindView(actionContext, viewName, isMainPage: true);
73+
if (findViewResult.Success)
74+
{
75+
return findViewResult.View;
76+
}
77+
78+
var searchedLocations = getViewResult.SearchedLocations.Concat(findViewResult.SearchedLocations);
79+
var errorMessage = string.Join(
80+
Environment.NewLine,
81+
new[] { $"Unable to find view '{viewName}'. The following locations were searched:" }.Concat(searchedLocations)); ;
82+
83+
throw new InvalidOperationException(errorMessage);
84+
}
85+
7086
private ActionContext GetActionContext()
7187
{
7288
var httpContext = new DefaultHttpContext();

0 commit comments

Comments
 (0)