예제 #1
0
        private ItemDefinition GetOrCreateDefinition(ContentRegistration re)
        {
            var definition = builder.GetDefinitions().FirstOrDefault(d => d.ItemType == re.ContentType)
                             ?? map.GetOrCreateDefinition(re.ContentType, re.Template);

            return(definition.Clone());
        }
예제 #2
0
        public virtual IEnumerable <ItemDefinition> Register(DefinitionMap map)
        {
            var registration = new ContentRegistration <T>(map.GetOrCreateDefinition(RegisteredType));

            registration.IsDefined = true;
            RegisterDefinition(registration);
            return(new [] { registration.Finalize() });
        }
예제 #3
0
        public virtual IEnumerable <ContentRegistration> AnalyzeViews(VirtualPathProvider vpp, HttpContextBase httpContext, IEnumerable <ViewTemplateSource> sources)
        {
            var registrations = new List <ContentRegistration>();

            foreach (var source in sources)
            {
                var virtualDirList = new List <string>
                {
                    Url.ResolveTokens(Url.ThemesUrlToken) + "Default/Views/" + source.ControllerName,
                    "~/Views/" + source.ControllerName
                };

                virtualDirList.AddRange(
                    from virtualDirectory in vpp.GetDirectory("~/Areas/").Directories.Cast <VirtualDirectory>()
                    let virtualPath = String.Format("~/Areas/{0}/Views/{1}", virtualDirectory.Name, source.ControllerName)
                                      select virtualPath);

                foreach (var virtualDir in virtualDirList.Where(vpp.DirectoryExists))
                {
                    logger.Debug(String.Format("Analyzing directory {0}", virtualDir));
                    foreach (var file in vpp.GetDirectory(virtualDir).Files.OfType <VirtualFile>())
                    {
                        Debug.Assert(file.Name != null, "file.Name != null");
                        if (!file.Name.EndsWith(source.ViewFileExtension) || file.Name.StartsWith("_"))
                        {
                            logger.Info(String.Format("Skipping file {0}", file.VirtualPath));
                            continue;
                        }
                        logger.Debug(String.Format("Analyzing file {0}", file.VirtualPath));

                        ContentRegistration registration = null;
                        if (httpContext.IsDebuggingEnabled)
                        {
                            registration = AnalyzeView(httpContext, file, source.ControllerName, source.ModelType);
                        }
                        else
                        {
                            try
                            {
                                registration = AnalyzeView(httpContext, file, source.ControllerName, source.ModelType);
                            }
                            catch (Exception ex)
                            {
                                logger.Error(ex);
                            }
                        }

                        if (registration != null)
                        {
                            registrations.Add(registration);
                        }
                    }
                }
            }
            return(registrations);
        }
예제 #4
0
        public virtual IEnumerable <ContentRegistration> AnalyzeViews(VirtualPathProvider vpp, HttpContextBase httpContext, IEnumerable <ViewTemplateSource> sources)
        {
            var registrations = new List <ContentRegistration>();

            foreach (var source in sources)
            {
                string virtualDir = Url.ResolveTokens(Url.ThemesUrlToken) + "Default/Views/" + source.ControllerName;

                logger.DebugFormat("Analyzing directory {0}", virtualDir);

                if (!vpp.DirectoryExists(virtualDir))
                {
                    virtualDir = "~/Views/" + source.ControllerName;
                    if (!vpp.DirectoryExists(virtualDir))
                    {
                        continue;
                    }
                }

                foreach (var file in vpp.GetDirectory(virtualDir).Files.OfType <VirtualFile>().Where(f => f.Name.EndsWith(source.ViewFileExtension)))
                {
                    logger.DebugFormat("Analyzing file {0}", file.VirtualPath);

                    ContentRegistration registration = null;
                    if (httpContext.IsDebuggingEnabled)
                    {
                        registration = AnalyzeView(httpContext, file, source.ControllerName, source.ModelType);
                    }
                    else
                    {
                        try
                        {
                            registration = AnalyzeView(httpContext, file, source.ControllerName, source.ModelType);
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                        }
                    }
                    if (registration != null)
                    {
                        registrations.Add(registration);
                    }
                }
            }
            return(registrations);
        }
예제 #5
0
        private ViewTemplateDescription RenderViewForRegistration(VirtualFile file, Type modelType, ControllerContext cctx, ViewEngineResult result)
        {
            var re = new ContentRegistration();

            re.ContentType = modelType;
            re.TemplateKey = N2.Web.Url.RemoveAnyExtension(file.Name);
            re.IsDefined   = false;
            using (StringWriter sw = new StringWriter())
            {
                var vdd = new ViewDataDictionary();
                cctx.Controller.ViewData = vdd;
                N2.Web.Mvc.Html.RegistrationExtensions.SetRegistrationExpression(cctx.HttpContext, re);

                try
                {
                    result.View.Render(new ViewContext(cctx, result.View, vdd, new TempDataDictionary(), sw), sw);
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                    if (re.IsDefined)
                    {
                        throw;
                    }
                    return(null);
                }
                finally
                {
                    N2.Web.Mvc.Html.RegistrationExtensions.SetRegistrationExpression(cctx.HttpContext, null);
                }

                if (re.IsDefined)
                {
                    return(new ViewTemplateDescription
                    {
                        Registration = re,
                        Definition = GetOrCreateDefinition(re),
                        TouchedPaths = new[] { file.VirtualPath }.Union(re.TouchedPaths)
                    });
                }
                return(null);
            }
        }
예제 #6
0
        // ReSharper disable RedundantNameQualifier
        private ContentRegistration RenderViewForRegistration(VirtualFileBase file, Type modelType, ControllerContext cctx, ViewEngineResult result)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            // ReSharper disable once UseObjectOrCollectionInitializer
            var re = new ContentRegistration(map.CreateDefinition(modelType, N2.Web.Url.RemoveAnyExtension(file.Name)));

            re.IsDefined = false;
            re.Context.TouchedPaths.Add(file.VirtualPath);

            using (var sw = new StringWriter())
            {
                var vdd = new ViewDataDictionary();
                cctx.Controller.ViewData = vdd;
                N2.Web.Mvc.Html.RegistrationExtensions.SetRegistrationExpression(cctx.HttpContext, re);

                try
                {
                    logger.DebugFormat("Rendering view {0} for registrations", file.VirtualPath);
                    result.View.Render(new ViewContext(cctx, result.View, vdd, new TempDataDictionary(), sw), sw);
                    logger.DebugFormat("Rendered view {0}, editables = {1}, defined = {2}", file.VirtualPath, re.Definition.Editables.Count, re.IsDefined);
                }
                catch (Exception ex)
                {
                    logger.Error(file.VirtualPath, ex);
                    if (re.IsDefined)
                    {
                        throw new Exception(String.Format("Failed to render view {0} for registrations", file.VirtualPath), ex);
                    }
                    return(null);
                }
                finally
                {
                    N2.Web.Mvc.Html.RegistrationExtensions.SetRegistrationExpression(cctx.HttpContext, null);
                }

                return(re.IsDefined ? re : null);
            }
        }
 public Builder(string propertyName, ContentRegistration re)
     : this(re)
 {
     this.PropertyName = propertyName;
 }
 public Builder(ContentRegistration re)
 {
     this.Registration = re;
 }
예제 #9
0
 public InstanceBuilder(ContentRegistration re)
     : base(re)
 {
 }
예제 #10
0
 public EditableBuilder(string propertyName, ContentRegistration re)
     : base(propertyName, re)
 {
     this.PropertyName = propertyName;
     this.Registration = re;
 }
예제 #11
0
 public ContainerBuilder(string containerName, ContentRegistration <TModel> re)
     : base(containerName, re)
 {
     Registration = re;
 }
예제 #12
0
        private ItemDefinition GetOrCreateDefinition(ContentRegistration re)
        {
            var definition = map.GetOrCreateDefinition(re.ContentType, re.Template);

            return(definition);
        }
예제 #13
0
 public override RegisteringDisplayRenderer <T> Create <T>(N2.Web.Rendering.RenderingContext context, N2.Definitions.Runtime.ContentRegistration re)
 {
     return(new UnencodedRegisteringDisplayRenderer <T>(context, re));
 }