/// <summary> /// Render a static view page locally directly instead of rendering from database content. /// Note that every view should have a layout section defined (_ViewStart.cshtml will not work in this case) /// /// Folloring rendering rule applies: /// http://host:port/ --- ~/Views/Default.cshtml /// http://host:post/en-gb/ --- ~/Views/en-gb/Index.cshtml /// http://host:post/en-gb/mobile --- ~/Views/en-gb/mobile/Index.cshtml (if Mobile.cshtml is not found, fallback to ~/Views/en-gb/mobile/Index.cshtml /// -------------------------------------------------------------------------- following urls should have equivlent patterns /// http://host:post/en-gb/help/rules --- ~/Views/en-gb/help/rules.cshtml /// </summary> /// <param name="url"></param> /// <param name="mvcRequestContext"></param> /// <returns></returns> public CmsEngine.Response ProcessFakeRequest(Uri url, MvcRequestContext mvcRequestContext) { // look for culture strings var abs = (url.AbsolutePath == "/") ? "/Default" : url.AbsolutePath; var cultureRegex = new System.Text.RegularExpressions.Regex(@"^\/(?<culture>[a-zA-Z]{2}\-[a-zA-Z]{2})?\/?"); var result = cultureRegex.Match(url.AbsolutePath); var culture = result.Groups["culture"]; if (culture.Success && abs.EndsWith(culture.Value)) { abs += "/Index"; } var viewName = string.Format("~/Views{0}.cshtml", abs); // Fallback to Index.cshtml if abs file is not found if (!System.IO.File.Exists(Server.MapPath(viewName))) { viewName = string.Format("~/Views{0}.cshtml", abs + "/Index"); } var pageHtmlBuilder = mvcRequestContext.RenderRazorViewToString(viewName: viewName, languageCode: culture.Success ? culture.Value : "", model: new object()); if (pageHtmlBuilder == null) { return new CmsEngine.Response { Type = CmsEngine.ResponseType.PageNotFound } } ; return(new CmsEngine.Response { Body = pageHtmlBuilder.ToString(), Type = CmsEngine.ResponseType.OK }); }
public virtual ActionResult ProcessFakeAngularRequest() { if (AtPreviewMode() && InvalidCrossSiteAuth()) { return(new HttpUnauthorizedResult()); } var url = _httponContextBase.Request.Url; var mvcRequestContext = new MvcRequestContext(ControllerContext, ViewData, TempData); // look for culture strings var absolutePath = url.AbsolutePath.ToLower().Replace("/templates", "").Replace(".html", "").Replace(".htm", ""); var abs = (absolutePath == "/") ? "/Default" : absolutePath; var cultureRegex = new System.Text.RegularExpressions.Regex(@"^\/(?<culture>[a-zA-Z]{2}\-[a-zA-Z]{2})?\/?"); var result = cultureRegex.Match(absolutePath); var culture = result.Groups["culture"]; if (culture.Success && abs.EndsWith(culture.Value)) { abs += "/Index"; } var viewName = string.Format("~/Views{0}.cshtml", abs); // Fallback to Index.cshtml if abs file is not found if (!System.IO.File.Exists(Server.MapPath(viewName))) { viewName = string.Format("~/Views{0}.cshtml", abs + "/Index"); } var pageHtmlBuilder = mvcRequestContext.RenderRazorViewToString(viewName: viewName, languageCode: culture.Success ? culture.Value : "", model: new object()); if (pageHtmlBuilder == null) { return(HttpNotFound()); } else { var htmlStr = pageHtmlBuilder.ToString(); // Remove Layout definition var regex = new Regex("Layout\\s=\\s\\\".*\\.cshtml\";"); var newMarkup = regex.Replace(htmlStr, ""); return(Content(newMarkup)); } }