Exemplo n.º 1
0
        private ViewResult ShowWebsiteErrorPage()
        {
            try
            {
                string errorPageFileLocation = Configuration.CustomRouting.GetErrorPageFile();

                errorPageFileLocation = Globalization.GlobalizeView(errorPageFileLocation, Request);
                return(View(errorPageFileLocation)); // Custom error page from the theme will be served.
            }
            catch (Exception ex)
            {
                throw new CustomRoutingException(ex); // This will eventually raise the Fatal Error Page in Production mode.
            }
        }
Exemplo n.º 2
0
        public IActionResult Index()
        {
            try
            {
                // Test, whether the installation was already completed and database was seeded
                if (!Configuration.InstallationCompleted())
                {
                    return(RedirectToAction("Index", "Install", new { area = "admin" }));
                }

                string rawUrl = Request.Path; // Gets ie.: /MyController/MyActionName, WITHOUT HOST NAME

                // In the startup.cs there is: app.UseExceptionHandler("/Views/Shared/_FatalError.cshtml").
                // We don't want to look in custom rounting system for this url and just serve it as is.
                // This error view will be returned usually in case of syntax error in view file.
                TempData["ErrorInPageLevelCode"] = "1";
                if (rawUrl == "/Views/Shared/_FatalError.cshtml")
                {
                    return(View(rawUrl));
                }

                // Let's test whether we can connect to the database
                try
                {
                    // If there is any error with the connection, this statement will throw exception.
                    (((_dataServicePublic.Database as DatabaseContext).Database).GetService <IDatabaseCreator>() as RelationalDatabaseCreator).Exists();
                }
                catch (Exception)
                {
                    //return ShowWebsiteErrorPage(); --> this will show theme error page
                    TempData["ExceptionMessage"] = new DatabaseConnectionFailureException();
                    return(View("_FatalError", UpdateModel())); // This page shows different output for production setting
                }

                string file;                                                             // helper variable for storing physical file adress for custom routing system

                if (Configuration.CustomRouting.IsHomePage(rawUrl))                      // For the main (index) page only
                {
                    string viewToReturn = Configuration.CustomRouting.GetHomePageFile(); // throws CustomRouting exception when view is not found
                    viewToReturn = Globalization.GlobalizeView(viewToReturn, Request);   // current culture is fetched and requested view name changed to e.x. : Index.{culture}.cshtml
                    return(View(viewToReturn));
                }
                else if (!string.IsNullOrEmpty(file = Configuration.CustomRouting.MapUrlToFile(rawUrl))) // Custom mapping for non-index pages, throws exception if view is not found
                {
                    // if urlRewriting is disabled + request for article page + request has parameter Id => id has to be
                    // supplied for article view model

                    if (!Configuration.WebsiteConfig.UrlRewriting && UrlRewriting.CompareUrls(rawUrl, Configuration.WebsiteConfig.ArticleRoute))
                    {
                        string queryId;
                        if (!string.IsNullOrEmpty(queryId = Request.Query["id"].ToString()))
                        {
                            try
                            {
                                file = Globalization.GlobalizeView(file, Request);
                                return(View(file, Convert.ToInt32(queryId)));
                            }
                            catch
                            {
                                // program will continue
                            }
                        }
                    }


                    // the requested file could be something other than .cshtml view (robots.txt etc.)
                    if (file.EndsWith(".txt"))
                    {
                        return(Content(System.IO.File.ReadAllText(file)));
                    }

                    file = Globalization.GlobalizeView(file, Request);// current culture is fetched and requested view name changed to e.x. : About.{culture}.cshtml
                    return(View(file));
                }
                // --------------URL REWRITING FOR ARTICLES-----------------------------------------------------------------
                // first condition checks whether is URL rewriting allowed in theme jasper.json
                else if (Configuration.WebsiteConfig.UrlRewriting && UrlRewriting.IsUrlRewriteRequest(rawUrl))
                {
                    int articleIdFromRequest = UrlRewriting.ReturnArticleIdFromNiceUrl(rawUrl, _dataServicePublic);
                    if (articleIdFromRequest != -1) // appropriate articleId was found in the DB
                    {
                        string articleFileLocation  = Configuration.WebsiteConfig.ArticleFile;
                        string physicalFileLocation = Configuration.CustomRouting.RelativeThemePathToRootRelativePath(articleFileLocation);

                        physicalFileLocation = Globalization.GlobalizeView(physicalFileLocation, Request);
                        return(View(physicalFileLocation, articleIdFromRequest)); // this returns view based one path: ie: /Themes/Jasper/Article.cshtml and passes model data.
                    }
                    else // URL rewriting rule for current article request does not exist
                    {
                        // Return Article view and pass Null instead of Id --> view itself will handle the null value and show ie.: article not found
                        string articleFileLocation  = Configuration.WebsiteConfig.ArticleFile;
                        string physicalFileLocation = Configuration.CustomRouting.RelativeThemePathToRootRelativePath(articleFileLocation);

                        physicalFileLocation = Globalization.GlobalizeView(physicalFileLocation, Request);
                        return(View(physicalFileLocation, null));
                    }
                }    // --------------END URL REWRITING FOR ARTICLES--------------------------------------------------------------
                else // page was not found
                {
                    bool isRequestFromAdminArea = (rawUrl.ToLower()).StartsWith("/admin", false, System.Globalization.CultureInfo.CurrentCulture) ? true : false;
                    if (isRequestFromAdminArea)
                    {
                        // Admin error page
                        if (!Env.Hosting.IsDevelopment())
                        {
                            return(RedirectToAction("Error", "Home", new { area = "admin" })); // URL will be changed
                        }
                        //  return View("~/Areas/Admin/Views/Shared/_Error.cshtml"); // URL will remain the same
                        else
                        {
                            return(View());
                        }
                    }
                    else
                    {
                        // Website error page
                        if (!Env.Hosting.IsDevelopment())
                        {
                            return(ShowWebsiteErrorPage());
                        }
                        else
                        {
                            return(View()); // ASP.NET error page will be shown
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TempData["ExceptionMessage"] = ex;

                return(View("_FatalError", UpdateModel()));
            }
        }