private void ExecuteTemplateRendering(TextWriter sw, IPublishedRequest request) { var httpContext = _httpContextAccessor.GetRequiredHttpContext(); // isMainPage is set to true here to ensure ViewStart(s) found in the view hierarchy are rendered var viewResult = _viewEngine.GetView(null, $"~/Views/{request.GetTemplateAlias()}.cshtml", isMainPage: true); if (viewResult.Success == false) { throw new InvalidOperationException($"A view with the name {request.GetTemplateAlias()} could not be found"); } var viewData = new ViewDataDictionary(_modelMetadataProvider, new ModelStateDictionary()) { Model = request.PublishedContent }; var writer = new StringWriter(); var viewContext = new ViewContext( new ActionContext(httpContext, httpContext.GetRouteData(), new ControllerActionDescriptor()), viewResult.View, viewData, _tempDataDictionaryFactory.GetTempData(httpContext), writer, new HtmlHelperOptions() ); viewResult.View.RenderAsync(viewContext).GetAwaiter().GetResult(); var output = writer.GetStringBuilder().ToString(); sw.Write(output); }
public async Task Match_Document_By_Url_With_Template(string urlAsString) { var globalSettings = new GlobalSettings { HideTopLevelNodeFromPath = false }; var template1 = CreateTemplate("test"); var template2 = CreateTemplate("blah"); var umbracoContext = GetUmbracoContext(urlAsString, template1.Id, globalSettings: globalSettings); var publishedRouter = CreatePublishedRouter(GetUmbracoContextAccessor(umbracoContext)); var reqBuilder = await publishedRouter.CreateRequestAsync(umbracoContext.CleanedUmbracoUrl); var webRoutingSettings = new WebRoutingSettings(); var lookup = new ContentFinderByUrlAndTemplate( LoggerFactory.CreateLogger <ContentFinderByUrlAndTemplate>(), ServiceContext.FileService, ServiceContext.ContentTypeService, GetUmbracoContextAccessor(umbracoContext), Microsoft.Extensions.Options.Options.Create(webRoutingSettings)); var result = lookup.TryFindContent(reqBuilder); IPublishedRequest frequest = reqBuilder.Build(); Assert.IsTrue(result); Assert.IsNotNull(frequest.PublishedContent); var templateAlias = frequest.GetTemplateAlias(); Assert.IsNotNull(templateAlias); Assert.AreEqual("blah".ToUpperInvariant(), templateAlias.ToUpperInvariant()); }
private string?GetTemplateName(IPublishedRequest request) { // check that a template is defined), if it doesn't and there is a hijacked route it will just route // to the index Action if (request.HasTemplate()) { // the template Alias should always be already saved with a safe name. // if there are hyphens in the name and there is a hijacked route, then the Action will need to be attributed // with the action name attribute. return(request.GetTemplateAlias()?.Split('.')[0].ToSafeAlias(_shortStringHelper)); } return(null); }
/// <inheritdoc /> public async Task <UmbracoRouteValues> CreateAsync(HttpContext httpContext, IPublishedRequest request) { if (httpContext is null) { throw new ArgumentNullException(nameof(httpContext)); } if (request is null) { throw new ArgumentNullException(nameof(request)); } string?customActionName = null; // check that a template is defined), if it doesn't and there is a hijacked route it will just route // to the index Action if (request.HasTemplate()) { // the template Alias should always be already saved with a safe name. // if there are hyphens in the name and there is a hijacked route, then the Action will need to be attributed // with the action name attribute. customActionName = request.GetTemplateAlias()?.Split('.')[0].ToSafeAlias(_shortStringHelper); } // The default values for the default controller/action var def = new UmbracoRouteValues( request, _defaultControllerDescriptor.Value, customActionName); def = CheckHijackedRoute(httpContext, def, out var hasHijackedRoute); def = await CheckNoTemplateAsync(httpContext, def, hasHijackedRoute); return(def); }