Ability to provide `RazorViewEngineOptions` to the `RazorTemplateEngine` before Render.
rahulvermapropertyme opened this issue · 5 comments
This project seems pretty good and does what it is supposed to do but I was wondering if we can have a way to supply RazorViewEngineOptions
from outside before we call Render?
Given RazorTemplateEngine
class is static it makes it hard to initialise the class with the RazorViewEngineOptions
as an optional dependency.
A potential not-so-pretty solution to this could be to utilise Initialize
method on this static class to optionally set the options?
I kind of got the not-so-pretty working version of it but looking for some thoughts/input on this feature request.
Hey @rahulvermapropertyme, thanks for raising this issue. I've not used RazorViewEngineOptions
much. But could you please elaborate on what's the use case for introducing this optional param?
Hi @soundaranbu - RazorViewEngineOptions
provides you ability to specify area and view page locations and formats (https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.razor.razorviewengineoptions?view=aspnetcore-8.0).
In my use case, I am working with an application which has ViewPages (.cshtml) in several non-standard view locations. So being unable to pass the RazorViewEngineOptions
from outside makes the RazorViewEngine
unable to find the views. In some cases, I have partial views stored in different locations leading to the same problem.
Passing the RazorViewEngineOptions
from outside allows me to tell the RazorViewEngine
about the location and format of my view pages.
RazorTemplateEngine
class is currently a static class and lazy loads the IRazorTemplateEngine
_instance without any parameters from outside. This makes it a little challenging to refactor. The current workaround for this I got is to utilise the Initialize()
constructor to pass in and set the RazorViewEngineOptions
before calling the Render* methods.
As I said in my previous comment, here is the not-so-pretty commit containg those changes..
8396b64
Please provide feedback, I am happy to refactor the code based on what you think.
Ah I see.. have you tried the DI approach mentioned in the README?
Basically you should be able to configure the option like this https://www.c-sharpcorner.com/blogs/custom-razor-view-location-in-asp-net-core-mvc
But please let me know if there are any difficulties in it
I see. So what we can do is configure these options on the ServiceCollection
before adding razor templating(AddRazorTemplating
).
In my case, I did not have the ServiceCollection
to begin with (it's a test project) but nothing stopping me initialising a new ServiceCollection
as it will be used by the the RazorViewEngine anyway.
So I can do something like this and it works.
var services = new ServiceCollection();
services.Configure<RazorViewEngineOptions>(SetupRazorViewEngineOptions);
services.AddRazorTemplating();
Thanks for your help! I am happy to close this "Feature Request".