November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
Hi,
Sure you can. Put the following code in your template's base class and call the method from page load event (or similar):
private void RedirectSimpleAddress() { if (PageEditing.PageIsInEditMode) { return; } string simpleAddress = this.CurrentPage.ExternalURL; if (!string.IsNullOrWhiteSpace(simpleAddress)) { var requestedPath = this.Request.RawUrl; if (!simpleAddress.StartsWith("/")) { simpleAddress = string.Concat("/", simpleAddress); } simpleAddress = VirtualPathUtility.RemoveTrailingSlash(simpleAddress); requestedPath = VirtualPathUtility.RemoveTrailingSlash(requestedPath); if (requestedPath.Equals(simpleAddress, StringComparison.OrdinalIgnoreCase)) { this.Response.RedirectPermanent(this.CurrentPage.ExternalUrl(true)); } } }
ExternalUrl(true) is an extension method to retrieve the url of the page, which I didn't include in this code example. This is untested code, but something similar should work.
Thanks Johan! That pointed me in the right direction. I'm using MVC so I put a similar method in a IResultFilter's OnResultExecuting method and it works perfect! :-)
Hi Daniel,
can you please post the sample code how you did this.. a client wants this also..
thanks in advance
public class PageContextActionFilter : IResultFilter { private readonly PageViewContextFactory _contextFactory; public PageContextActionFilter(PageViewContextFactory contextFactory) { _contextFactory = contextFactory; } public void OnResultExecuting(ResultExecutingContext filterContext) { var viewModel = filterContext.Controller.ViewData.Model; var model = viewModel as IPageViewModel<BlankPageData>; if (model != null) { var currentContentLink = filterContext.RequestContext.GetContentLink(); model.CurrentPage.PageRouteValues = filterContext.RequestContext.GetPageRoute(currentContentLink); RedirectIfCurrentUrlIsSimpleAddress(filterContext.RequestContext, model.CurrentPage); var layoutModel = model.Layout ?? _contextFactory.CreateLayoutModel(currentContentLink, filterContext.RequestContext, model.CurrentPage); var layoutController = filterContext.Controller as IModifyLayout; if (layoutController != null) { layoutController.ModifyLayout(layoutModel); } model.Layout = layoutModel; if (model.Section == null) { model.Section = _contextFactory.GetSection(currentContentLink); } } } private void RedirectIfCurrentUrlIsSimpleAddress(RequestContext requestContext, BlankPageData currentPage) { if (PageEditing.PageIsInEditMode) return; string simpleAddress = currentPage.ExternalURL; if (!string.IsNullOrWhiteSpace(simpleAddress)) { var requestedPath = requestContext.HttpContext.Request.RawUrl; if (!simpleAddress.StartsWith("/")) { simpleAddress = string.Concat("/", simpleAddress); } if (!simpleAddress.EndsWith("/")) { simpleAddress = string.Format("{0}/", simpleAddress); } if (string.IsNullOrEmpty(requestedPath)) return; if (requestedPath.Equals(simpleAddress, StringComparison.OrdinalIgnoreCase)) { // Should redirect var fullUrl = currentPage.PageLink.GetUrl(); if (!string.IsNullOrEmpty(fullUrl)) { if (fullUrl == simpleAddress) return; requestContext.HttpContext.Response.RedirectPermanent(fullUrl); } } } } public void OnResultExecuted(ResultExecutedContext filterContext) { } }
Then you'll need to register it in an IInitalizableModule,
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))] public class FilterConfig : IInitializableModule { public void Initialize(InitializationEngine context) { GlobalFilters.Filters.Add(ServiceLocator.Current.GetInstance<PageContextActionFilter>()); } public void Uninitialize(InitializationEngine context) { } public void Preload(string[] parameters) { } }
Some of the above code is specific for my solution but I'll hope it will get you in the right direction. Let me know if you have any questions.
Thanks man...
Because i didnt use the PageContextActionFilter from the Alloy demo and i dont use the ModifyLayout stuff, i added this to my PageControllerBase<T> OnActionExecuting method and this works perfectly..
Hi,
Is it possible to make a 301 redirect to a pages full url when someone is making a request to a simple address?
Thanks!