RickStrahl / Westwind.RazorHosting

Hosting the Razor Runtime outside of ASP.NET/MVC for use in non-Web .NET applications.
144 stars 32 forks source link

Usage with ASP WebApi #37

Open Cristiian opened 5 years ago

Cristiian commented 5 years ago

Hello, I'm trying to use this library for email templating in ASP web Api, I'm using the UseAppdomain Option, but is unable to load the Westwind.Razor dll, I have set the BaseBinaryFolder to the path where all the dll's exists, but the error is the same, if I use the UseAppdomain = false, everything works fine, I tried this with a console application, and works with no issue

Web Api Structure (root path) c:\...\ imagen

this is the class for the renderer I'm Using

public class RazorTemplates : IDisposable
    {
        RazorFolderHostContainer Host { get; set; }

        public RazorTemplates()
        {
            string ServerMapPath = HttpContext.Current.Server.MapPath("~"); // c:\\...\ root path

            Host = new RazorFolderHostContainer()
            {
                // *** Set your Folder Path here - physical or relative ***
                TemplatePath = Path.Combine(ServerMapPath, "Views", "Email"),//c:\\...\Views\Email
                // *** Path to the Assembly path of your application
                BaseBinaryFolder = Path.Combine(ServerMapPath, "bin"), //c:\\...\bin
                UseAppDomain = true,
                ThrowExceptions = true
            };

            Host.Start();
        }

        public string RenderTemplate(string template, object model)
        {
            string result = Host.RenderTemplate(template, model);
            if (result == null)
            {
                throw new InvalidOperationException(Host.ErrorMessage);
            }
            return result;
        }

        public void Dispose()
        {
            Host.Stop();
            Host.Dispose();
        }
    }

Rendering a template

            using (RazorTemplates host = new RazorTemplates())
            {
                return Ok(host.RenderTemplate("~/_PartialPage1.cshtml", new { }));
            }

The exception:

imagen