Sitecore 通过 processor 来自定义类似 github 的 not found 页面
1.Sitecore 应用与介绍2.Sitecore 下载与安装3.Sitecore Nuget 配置4.Sitecore Lincense 的更新5.Sitecore 打包备份与恢复6.Sitecore Field 类型与 C# 类型映射表7.Sitecore DI8.Sitecore EXM 的使用9.在 Sitecore 里使用 Solr 搜索 SortOrder 关联的 Item10. Sitecore 里删除 Item 报错 Could not create SSL/TLS secure channel.11.Sitecore 优化,加快应用每次重新 loading 速度。12.Sitecore 扩展CM部署13.Sitecore FieldRenderer14.Sitecore MVC @Html.RenderSection() 的替代方案15.Sitecore Query String Parameters16.Sitecore Item CURD17.Sitecore ListManagaer Operation18.Sitecore 通过 AutomatedMessage 发送邮件19.Sitecore CD ShowConfig.aspx20.Sitecore DataSource Query21.Sitecore Form 的使用22.Sitecore ListManager Contact Lists vs Segment Lists
23.Sitecore 通过 processor 来自定义类似 github 的 not found 页面
有一个需求是类似 github 的 404 页面,当访问不存在的页面时,需要满足以下几点:
- 不是通过redirect或其他状态码让浏览器来跳转到到404页面;
- 链接还是原来链接,但是页面内容是 404;
- 由于是MVC模式,功能由 back-end 来实现;
- 状态码得是 404。
在基于 sitecore 的框架上,使用 sitecore 的 processor 来实现此功能:
文档地址:https://doc.sitecore.com/xp/en/developers/latest/sitecore-experience-manager/mvc-and-pipelines.html#mvc-specific-processors
using System.Net; using System.Web; using Sitecore.Diagnostics; using Sitecore.Layouts; using Sitecore.Pipelines.HttpRequest; using Sitecore.Web; namespace SitecoreConsole { public class NotFoundProcessor : Sitecore.Pipelines.HttpRequest.HttpRequestProcessor { public string ItemNotFoundItemPath { get; set; } public string MapContentItemPath { get; set; } public override void Process(HttpRequestArgs args) { string siteStartPath = Sitecore.Context.Site.StartPath; PageContext page = Sitecore.Context.Page; string filePath = page?.FilePath; if (Sitecore.Context.Item != null || Sitecore.Context.Site == null || Sitecore.Context.Database == null) { return; } if (Sitecore.Context.Item == null) { string fullUrl = WebUtil.GetFullUrl(WebUtil.GetRawUrl()); string cacheKey = $"cacheKey_{fullUrl}"; string isActiveCache = string.Empty; if (Sitecore.Context.Site.CacheHtml && (isActiveCache = Sitecore.Context.Site.Caches.HtmlCache.GetHtml(cacheKey)) == "active") { return; } // 这个是处理一个 AutoLinkMap,将一个链接映射到另一个链接的程序,此处可以是不到此功能 // if (string.IsNullOrEmpty(isActiveCache)) // { // var mapContentItem = Sitecore.Context.Database.GetItem(string.Concat(siteStartPath, MapContentItemPath)); // if (mapContentItem != null) // { // if (!string.IsNullOrEmpty(mapContentItem.Fields["XML Mapping"]?.Value)) // { // string mapContent = mapContentItem.Fields["XML Mapping"].Value; // if (mapContent.IndexOf($"<oldlink>{fullUrl}</oldlink>", StringComparison.OrdinalIgnoreCase) != -1) // { // if (Sitecore.Context.Site.CacheHtml) // { // Sitecore.Context.Site.Caches.HtmlCache.SetHtml(cacheKey, "active"); // } // // return; // } // } // } // // if (Sitecore.Context.Site.CacheHtml) // { // Sitecore.Context.Site.Caches.HtmlCache.SetHtml(cacheKey, "disabled"); // } // } var notFoundItem = Sitecore.Context.Database.GetItem(string.Concat(siteStartPath, ItemNotFoundItemPath)); if (notFoundItem != null) { HttpContextBase httpContext = args.HttpContext; if (httpContext != null) { httpContext.Items["PageNotFound"] = 1; } Sitecore.Context.Item = notFoundItem; Log.Debug("[NotFoundProcessor] Set to item " + notFoundItem.Paths.FullPath, this); } else { Log.Debug("[NotFoundProcessor] notFoundItem not found", this); } } } } public class SetHttpResponseCode : HttpRequestProcessor { public override void Process(HttpRequestArgs args) { HttpContextBase httpContext = args.HttpContext; if (httpContext != null) { if (Sitecore.MainUtil.GetBool(httpContext.Items["PageNotFound"], false)) { HttpContext.Current.Response.TrySkipIisCustomErrors = true; HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotFound; } } } } }
配置文件
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" xmlns:role="http://www.sitecore.net/xmlconfig/role/"> <sitecore role:require="ContentDelivery"> <pipelines> <httpRequestBegin> <processor type="SitecoreConsole.NotFoundProcessor, SitecoreConsole" patch:before="processor[@type='Sitecore.Pipelines.HttpRequest.LayoutResolver, Sitecore.Kernel']"> <ItemNotFoundItemPath>/404</ItemNotFoundItemPath> <!--<LinkMapContentItemPath>/Map Content</LinkMapContentItemPath>--> </processor> </httpRequestBegin> <httpRequestEnd> <processor type="SitecoreConsole.SetHttpResponseCode, SitecoreConsole" patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.EndDiagnostics, Sitecore.Kernel']" /> </httpRequestEnd> </pipelines> </sitecore> </configuration>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步