Reputation: 21
I'm using Razor with emails templates in .NET core 3.
While I can get this all to works, I'd like to change my templates file (called EmailLayout.cshtml and located at /Pages/Shared/) as an Embedded resource. Whenever I do that in VS, I got this error:
InvalidOperationException: The layout view 'EmailLayout' could not be located. The following locations were searched: /Views/EmailLayout.cshtml /Views/Shared/EmailLayout.cshtml /Pages/Shared/EmailLayout.cshtml
Why my file couldn't be found? What should I add to get this to work?
Upvotes: 0
Views: 811
Reputation: 21
Finally found the answer.
1) Install Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation nuget package
2) Add this to configuration in Startup :
services.Configure<MvcRazorRuntimeCompilationOptions>(options => {
options.FileProviders.Clear();
options.FileProviders.Add(new EmbeddedFileProvider(appDirectory));
});
3) Change the service to services.AddRazorPages().AddRazorRuntimeCompilation();
Upvotes: 2
Reputation: 36605
Make sure that the Build Action
of the file is set to Content
in the properties
of the View.
You could follow the steps to use EmailLayout.cshtml
:
2.Be sure it is in /Pages/Shared
folder:
3.EmailLayout should be like:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
</head>
<body>
<h2>EmailLayout</h2>
<div>
@RenderBody()
</div>
</body>
</html>
4.Add the following code to your Razor Pages or _ViewStart.cshtml
:
@{
Layout = "EmailLayout";
}
Upvotes: 1