我在此路径中有此cshtml文件
"~/Areas/Services/Views/Quotations/SpecificForms/PC/PCReceipts.cshtml"
我正在尝试将其呈现为字符串并将一个viewmodel传递给它。
目前,我正在使用Nuget的RazorLight v1.1.0,这是我到目前为止尝试过的:
var tempatePath = "~/Areas/Services/Views/Quotations/SpecificForms/PC/PCReceipts.cshtml";
IRazorLightEngine engine = EngineFactory.CreatePhysical(templatePath);
但是,当我运行它时,出现一个错误,提示我需要一个绝对路径。如何将当前的内容转换为绝对路径?如果我给它一个绝对路径,那么当我编译并运行程序时,绝对路径会消失吗?
答案 0 :(得分:1)
注入IHostingEnvironment
并使用_env.ContentRootPath
:
public class FooController : Controller
{
private readonly IHostingEnvironment _env;
public FooController(IHostingEnvironment env)
{
_env = env;
}
public IActionResult FooAction()
{
var tempatePath = Path.Combine(_env.ContentRootPath, "Areas/Services/Views/Quotations/SpecificForms/PC/PCReceipts.cshtml");
IRazorLightEngine engine = EngineFactory.CreatePhysical(templatePath);
...
}
}