public void Configure(IAppHost appHost)
        {
            this.AppHost = appHost;
            appHost.ViewEngines.Add(this);

            //Default to watching modfied pages in DebugMode
            if (!WatchForModifiedPages)
            {
                WatchForModifiedPages = appHost.Config.DebugMode;
            }

            //Default to parallel execution in DebugMode
            if (this.TemplateProvider.CompileInParallelWithNoOfThreads <= 0)
            {
                this.TemplateProvider.CompileInParallelWithNoOfThreads = appHost.Config.DebugMode
                    ? Environment.ProcessorCount * 2
                    : 0;
            }

            foreach (var ns in EndpointHostConfig.RazorNamespaces)
            {
                TemplateNamespaces.Add(ns);
            }

            this.ReplaceTokens = appHost.Config.HtmlReplaceTokens ?? new Dictionary <string, string>();
            var webHostUrl = appHost.Config.WebHostUrl;

            if (!webHostUrl.IsNullOrEmpty())
            {
                this.ReplaceTokens["~/"] = webHostUrl.WithTrailingSlash();
            }

            if (VirtualPathProvider == null)
            {
                VirtualPathProvider = AppHost.VirtualPathProvider;
            }

            Init();

            RegisterRazorPages(appHost.Config.WebHostPhysicalPath);

            appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) => {
                ViewPageRef razorPage = null;

                if (catchAllPathsNotFound.Contains(pathInfo))
                {
                    return(null);
                }

                razorPage = FindByPathInfo(pathInfo);

                if (WatchForModifiedPages)
                {
                    ReloadIfNeeeded(razorPage);
                }

                if (razorPage == null)
                {
                    foreach (var entry in RazorExtensionBaseTypes)
                    {
                        if (pathInfo.EndsWith("." + entry.Key))
                        {
                            pathInfo = pathInfo.EndsWith(DefaultPage + "." + entry.Key, StringComparison.InvariantCultureIgnoreCase)
                                ? pathInfo.Substring(0, pathInfo.Length - (DefaultPage + "." + entry.Key).Length)
                                : pathInfo.WithoutExtension();

                            return(new RedirectHttpHandler {
                                AbsoluteUrl = webHostUrl.IsNullOrEmpty()
                                    ? null
                                    : webHostUrl.CombineWith(pathInfo),
                                RelativeUrl = webHostUrl.IsNullOrEmpty()
                                    ? pathInfo
                                    : null
                            });
                        }
                    }

                    if (catchAllPathsNotFound.Count > 1000) //prevent DDOS
                    {
                        catchAllPathsNotFound = new HashSet <string>();
                    }

                    var tmp = new HashSet <string>(catchAllPathsNotFound)
                    {
                        pathInfo
                    };
                    catchAllPathsNotFound = tmp;
                    return(null);
                }

                return(new RazorHandler(pathInfo)
                {
                    RazorFormat = this,
                    RazorPage = razorPage,
                    RequestName = "RazorPage",
                });
            });
        }
Example #2
0
        public void Configure(IAppHost appHost)
        {
            this.AppHost = appHost;
            appHost.ViewEngines.Add(this);

            foreach (var ns in EndpointHostConfig.RazorNamespaces)
            {
                TemplateNamespaces.Add(ns);
            }

            this.ReplaceTokens = appHost.Config.HtmlReplaceTokens ?? new Dictionary <string, string>();
            var webHostUrl = appHost.Config.WebHostUrl;

            if (!webHostUrl.IsNullOrEmpty())
            {
                this.ReplaceTokens["~/"] = webHostUrl.WithTrailingSlash();
            }

            if (VirtualPathProvider == null)
            {
                VirtualPathProvider = AppHost.VirtualPathProvider;
            }

            Init();

            RegisterRazorPages(appHost.Config.WebHostPhysicalPath);

            appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) => {
                ViewPageRef razorPage = null;

                if (catchAllPathsNotFound.Contains(pathInfo))
                {
                    return(null);
                }

                razorPage = FindByPathInfo(pathInfo);

                if (WatchForModifiedPages)
                {
                    ReloadModifiedPageAndTemplates(razorPage);
                }

                if (razorPage == null)
                {
                    foreach (var entry in RazorExtensionBaseTypes)
                    {
                        if (pathInfo.EndsWith("." + entry.Key))
                        {
                            return(new RedirectHttpHandler {
                                AbsoluteUrl = webHostUrl.IsNullOrEmpty()
                                    ? null
                                    : webHostUrl.CombineWith(pathInfo.WithoutExtension()),
                                RelativeUrl = webHostUrl.IsNullOrEmpty()
                                    ? pathInfo.WithoutExtension()
                                    : null
                            });
                        }
                    }

                    if (catchAllPathsNotFound.Count > 1000) //prevent DDOS
                    {
                        catchAllPathsNotFound = new HashSet <string>();
                    }

                    var tmp = new HashSet <string>(catchAllPathsNotFound)
                    {
                        pathInfo
                    };
                    catchAllPathsNotFound = tmp;
                    return(null);
                }

                return(new RazorHandler(pathInfo)
                {
                    RazorFormat = this,
                    RazorPage = razorPage,
                    RequestName = "RazorPage",
                });
            });
        }