soundaranbu / Razor.Templating.Core

Razor Templating Engine to render Razor Views(.cshtml files) to String in Console, Web, Service, Desktop workloads in .NET Core 3+

Home Page:https://soundaranbu.medium.com/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way we can specify an absolute path when calling RenderAsync

PonchoPowers opened this issue · comments

Is there a way we can specify an absolute path when calling RenderAsync?

When I try this at the moment I get an error:

System.InvalidOperationException: 'Unable to find view 'C:\USERS\TEST\SOURCE\REPOS\TEST\TEST.WEBSITE.WEBPAGES\Index.cshtml'. The following locations were searched:
/C:\USERS\TEST\SOURCE\REPOS\TEST\TEST.WEBSITE.WEBPAGES\Index.cshtml

It looks like the prepended / is causing the issue.

Hi @BonnieSoftware, upto my knowledge absolute path is not supported and it's not recommended as well. Only relative path to the project is supported.
May I know your use case?

Hi, the Razor Templating library is very useful for creating shared templates that are used across multiple websites, and will likely be stored on one or more drives, so being able to specify an absolute path would be good. Another use case is when creating a command line tool and wanting to pass in an argument to specify the location of the razor template to build, this was easy to achieve in ASP.NET 4.5 but with the advent of file providers it appears to be a lot more difficult to accomplish now.

Ok.. In this library all the views are compiled at the build time using Razor SDK. No runtime compilation happening. So for sharing the same views across multiple websites, you can consider referencing the same RCL project across different applications & specify the relative path ( like I've done here )

Build time compilation is what I was trying to achieve. I'm effectively using Razor (without MVC) to produce a static HTML site, so using a limited subset of features. I could see you were using the Razor Engine instead of reinventing the wheel which is why I was attracted to this project. The tool I was working on would have been ran on the command line so would have no knowledge of projects, so I can't reference another project. I could root a path provider to a local drive such as C: but what if someone wants to run my tool from C: to compile some templates on another drive such as D: so I really need to be able to use absolute paths, just didn't want to go down the route of making lots of changes to get this working.

Hi @BonnieSoftware. I just got a chance to try this scenario. The below snippet works in v1.7.0. I know it's late. But if you're still looking for this, you can try it and let me know how it works for you. Thanks!

using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Razor.Templating.Core;

var services = new ServiceCollection();
services.AddMvcCore().AddRazorRuntimeCompilation();
services.Configure<MvcRazorRuntimeCompilationOptions>(opts =>
{
    opts.FileProviders.Add(new PhysicalFileProvider(@"D:\PathToRazorViews")); // This will be the root path
});
services.AddRazorTemplating();

var html = await RazorTemplateEngine.RenderAsync("/Views/Home/Rcl.cshtml"); // relative path to the root

Note: ViewModel, ViewData, ViewBag can also be passed

commented

@soundaranbu used it inside a console application and its working. Thanks!