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

Ability to provide RazorViewEngineOptions to the RazorTemplateEngine before Render. #67

Closed
rahulvermapropertyme opened this issue Mar 7, 2024 · 5 comments

Comments

@rahulvermapropertyme
Copy link

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?

@rahulvermapropertyme
Copy link
Author

I kind of got the not-so-pretty working version of it but looking for some thoughts/input on this feature request.

@soundaranbu
Copy link
Owner

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?

@rahulvermapropertyme
Copy link
Author

rahulvermapropertyme commented Mar 7, 2024

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.

@soundaranbu
Copy link
Owner

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

@rahulvermapropertyme
Copy link
Author

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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants