1

I have this cshtml file located in this path

"~/Areas/Services/Views/Quotations/SpecificForms/PC/PCReceipts.cshtml"

I am trying to render this as a string and pass a viewmodel to it.

Currently I am using RazorLight v1.1.0 from Nuget and this is what I have tried so far:

var tempatePath = "~/Areas/Services/Views/Quotations/SpecificForms/PC/PCReceipts.cshtml";
IRazorLightEngine engine = EngineFactory.CreatePhysical(templatePath);

However when I run it, I get an error saying that I require an absolute path instead. How can I convert what I have currently to an absolute path? If I give it an absolute path, when I compile and run the program, won't the absolute path disappear?

1 Answer 1

1

Inject IHostingEnvironment and use _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);

        ...
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi! Thanks for answering. Does it work when i publish the application as well? That is what I'm worried about

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.