Ejemplo n.º 1
0
        public IActionResult Index(string id)
        {
            if (string.IsNullOrEmpty(id) || id == "Index")
            {
                return(RedirectToAction("Index", "Home"));
            }

            try
            {
                var postModel = _context.vwPostsApproved
                                .FirstOrDefault(m => m.PostPermalink == id);
                if (postModel == null)
                {
                    //check if PostPermalink contained in another PostPermalink
                    if (_context.vwPostsApproved.Any(s => s.PostPermalink.Contains(id)))
                    {
                        string PostPermalink = _context.vwPostsApproved.Where(s => s.PostPermalink.Contains(id)).OrderByDescending(s => s.ApprovalsDateAdded).FirstOrDefault().PostPermalink;
                        return(RedirectToAction("Index", "Posts", new { id = PostPermalink }));
                    }

                    //check if PostPermalink trimmed contained in another PostPermalink
                    id = id.Substring(0, id.Length - 10); //remove last 10 characters
                    if (_context.vwPostsApproved.Any(s => s.PostPermalink.Contains(id)))
                    {
                        string PostPermalink = _context.vwPostsApproved.Where(s => s.PostPermalink.Contains(id)).OrderByDescending(s => s.ApprovalsDateAdded).FirstOrDefault().PostPermalink;
                        return(RedirectToAction("Index", "Posts", new { id = PostPermalink }));
                    }
                    return(NotFound());
                }

                if (Convert.ToBoolean(functions.GetSiteLookupData("EnableFaceBookComments")))
                {
                    ViewData["FacebookCommentId"] = functions.GetSiteLookupData("FacebookCommentAppId");
                }

                ViewBag.FaceBookComments = Convert.ToBoolean(functions.GetSiteLookupData("EnableFaceBookComments"));

                string PostID = _context.vwPostsApproved.Where(s => s.PostPermalink == id).FirstOrDefault().PostID;
                ViewBag.PostID = PostID;
                string PostTitle  = _context.vwPostsApproved.Where(s => s.PostPermalink == id).FirstOrDefault().PostTitle;
                string PostAuthor = _context.vwPostsApproved.Where(s => s.PostPermalink == id).FirstOrDefault().PostAuthor;
                string PostType   = _context.vwPostsApproved.Where(s => s.PostPermalink == id).FirstOrDefault().PostType;
                string VisitorIP  = functions.FormatVisitorIP(_sessionManager.SessionIP, _accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString());
                string OtherInfo  = null; //add any other info here

                //log post view
                functions.LogPostView(PostID, PostAuthor, PostType, VisitorIP, _detectionService.Browser.Name.ToString(), _detectionService.Device.Type.ToString(), OtherInfo);

                //log visit
                functions.VisitLog(_systemConfiguration.visitLogTypes.Split(",")[1], PostTitle, VisitorIP, _detectionService.Browser.Name.ToString(), _detectionService.Device.Type.ToString(), null, OtherInfo);

                //get ShareThis url
                ViewBag.ShareThisUrl = functions.GetSiteLookupData("ShareThisUrl");

                ViewBag.ConnectionString = _systemConfiguration.connectionString;

                ViewData["Title"]              = PostTitle;
                ViewData["ContentKeywords"]    = postModel.MetaKeywords;
                ViewData["ContentDescription"] = PostTitle;
                ViewData["PostAuthor"]         = PostAuthor;

                //Set properties
                ViewData["PropertyDescription"] = "By " + functions.GetAccountData(PostAuthor, "FullName") + ", " + functions.FormatLongText(PostTitle, 120);
                ViewData["PropertySection"]     = _context.Categories.Where(s => s.CategoryID == postModel.PostCategory).FirstOrDefault().CategoryName;
                ViewData["PropertyUpdatedTime"] = postModel.UpdateDate;

                return(View(postModel));
            }
            catch (Exception ex)
            {
                //Log Error
                _logger.LogInformation("Get Post Details Error: " + ex.ToString());
                TempData["ErrorMessage"] = "There was an error processing your request. Please try again. If this error persists, please send an email.";
            }

            return(RedirectToAction("Index", "Home"));
        }