/// <summary> /// Construtor padrão. /// <param name="templateServiceConfiguration">Configuração do serviço</param> /// </summary> public RazorViewEngine(RazorEngine.Configuration.ITemplateServiceConfiguration templateServiceConfiguration) { templateServiceConfiguration.Require("templateServiceConfiguration").NotNull(); base.AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" }; base.AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" }; base.AreaPartialViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" }; base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" }; base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" }; base.PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" }; base.FileExtensions = new string[] { "cshtml", "vbhtml" }; _engineService = CreateRazorEngineService(templateServiceConfiguration); }
/// <summary> /// Inicializa a WebApplication. /// </summary> /// <param name="builder"></param> private void Startup(IAppBuilder builder) { var virtualPathProvider = new Hosting.VirtualFileProviderContainer(GetVirtualPathProviders()); System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(virtualPathProvider); var buildManagerField = typeof(System.Web.Compilation.BuildManager).GetField("_theBuildManager", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); object buildManager = null; if (buildManagerField != null) { buildManager = buildManagerField.GetValue(null); } var setType = typeof(System.Web.HttpRuntime).Assembly.GetType("System.Web.Util.StringSet"); if (setType != null) { var stringSetConstructor = setType.GetConstructors(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).FirstOrDefault(); if (stringSetConstructor != null) { var forbiddenTopLevelDirectories = setType.GetConstructors(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).First().Invoke(null); var forbiddenTopLevelDirectoriesField = typeof(System.Web.Compilation.BuildManager).GetField("_forbiddenTopLevelDirectories", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); if (forbiddenTopLevelDirectoriesField != null) { forbiddenTopLevelDirectoriesField.SetValue(buildManager, forbiddenTopLevelDirectories); } } } var preStartInitStageType = typeof(System.Web.HttpRuntime).Assembly.GetType("System.Web.Compilation.PreStartInitStage"); var preStartInitStageProperty = typeof(System.Web.Compilation.BuildManager).GetProperty("PreStartInitStage", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic); if (buildManager != null && preStartInitStageProperty != null && preStartInitStageType != null) { preStartInitStageProperty.SetValue(buildManager, Enum.Parse(preStartInitStageType, "AfterPreStartInit")); } if (buildManager != null) { typeof(System.Web.Compilation.BuildManager).GetMethod("Initialize", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).Invoke(buildManager, null); } if (buildManager != null && preStartInitStageProperty != null && preStartInitStageType != null) { var oldValue = preStartInitStageProperty.GetValue(buildManager); preStartInitStageProperty.SetValue(buildManager, Enum.Parse(preStartInitStageType, "DuringPreStartInit")); OnStart(); try { foreach (var i in _referenceAssemblies) { System.Web.Compilation.BuildManager.AddReferencedAssembly(i); } } finally { preStartInitStageProperty.SetValue(buildManager, oldValue); } } else { OnStart(); foreach (var i in _referenceAssemblies) { System.Web.Compilation.BuildManager.AddReferencedAssembly(i); } } var executePreAppStartMethod = typeof(System.Web.Compilation.BuildManager).GetMethod("ExecutePreAppStart", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); if (executePreAppStartMethod != null) { try { executePreAppStartMethod.Invoke(null, null); } catch (System.Reflection.TargetInvocationException ex) { throw ex.InnerException; } } System.Web.Mvc.ViewEngines.Engines.Clear(); System.Web.Mvc.ViewEngines.Engines.Add(_viewEngine = new Owin.Razor.RazorViewEngine(_templateServiceConfiguration = GetTemplateServiceConfiguration())); System.Web.Mvc.ValueProviderFactories.Factories.Remove(System.Web.Mvc.ValueProviderFactories.Factories.OfType <System.Web.Mvc.JsonValueProviderFactory>().FirstOrDefault()); System.Web.Mvc.ValueProviderFactories.Factories.Add(new Web.Mvc.JsonNetValueProviderFactory()); ConfigureJsonConvert(); ConfigureSignalR(builder); InitializeModules(); }
/// <summary> /// Recupera a instancia do serviço de template. /// </summary> /// <returns></returns> private RazorEngine.Templating.IRazorEngineService CreateRazorEngineService(RazorEngine.Configuration.ITemplateServiceConfiguration configuration) { return(RazorEngine.Templating.RazorEngineService.Create(configuration)); }