コード例 #1
0
        public void IsolatedRazorEngineService_Sandbox_WithISerializableModel()
        {
            using (var service = IsolatedRazorEngineService.Create(SandboxCreator))
            {
                const string template = "<h1>Uri Host: @Model.Host</h1>";
                const string expected = "<h1>Uri Host: example.com</h1>";

                var model  = new Uri("http://example.com");
                var result = service.RunCompile(template, "test", null, (object)RazorDynamicObject.Create(model));
                Assert.AreEqual(expected, result);
            }
        }
コード例 #2
0
        public void IsolatedRazorEngineService_ParseSimpleTemplate_WithExpandoModel()
        {
            using (var service = IsolatedRazorEngineService.Create())
            {
                const string template = "<h1>Animal Type: @Model.Type</h1>";
                const string expected = "<h1>Animal Type: Cat</h1>";

                dynamic model = new ExpandoObject();
                model.Type = "Cat";
                var result = service.RunCompile(template, "test", null, (object)RazorDynamicObject.Create(model));
                Assert.AreEqual(expected, result);
            }
        }
コード例 #3
0
        public void IsolatedRazorEngineService_CannotParseSimpleTemplate_WithDynamicModel()
        {
            using (var service = IsolatedRazorEngineService.Create())
            {
                const string template = "<h1>Animal Type: @Model.Type</h1>";
                const string expected = "<h1>Animal Type: Cat</h1>";

                dynamic model = new ValueObject(new Dictionary <string, object> {
                    { "Type", "Cat" }
                });
                string result = service.RunCompile(template, "test", null, (object)RazorDynamicObject.Create(model));
                Assert.AreEqual(expected, result);
            }
        }
コード例 #4
0
        public static string Razor(this string template, object model)
        {
            var m = RazorDynamicObject.Create(model, true);

            var key = template.To16bitMD5();

            if (Engine.Razor.IsTemplateCached(key, null))
            {
                return(Engine.Razor.Run(key, null, m));
            }
            else
            {
                return(Engine.Razor.RunCompile(template, key, null, m));
            }
        }
コード例 #5
0
        static void Main(string[] args)
        {
            using (var service = IsolatedRazorEngineService.Create())
            {
                const string template = "<h1>Animal Type: @Model.Type</h1>";
                const string expected = "<h1>Animal Type: Cat</h1>";

                dynamic model = new ExpandoObject();
                model.Type = "Cat";
                var result = service.RunCompile(template, "test", null, (object)RazorDynamicObject.Create(model));
                if (!Equals(expected, result))
                {
                    throw new Exception(string.Format("{0} expected but got {1}", expected, result));
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// Checks if we need to wrap the given model in
        /// an <see cref="RazorDynamicObject"/> instance and wraps it.
        /// </summary>
        /// <param name="modelType">the model-type</param>
        /// <param name="original">the original model</param>
        /// <param name="allowMissing">true when we should allow missing properties on dynamic models.</param>
        /// <returns>the original model or an wrapper object.</returns>
        internal static object GetDynamicModel(Type modelType, object original, bool allowMissing)
        {
            object result = original;

            if (modelType == null && original != null)
            {
                // We try to make some things work:
                if (CompilerServicesUtility.IsAnonymousTypeRecursive(original.GetType()))
                {
                    // TODO: we should handle Configuration.AllowMissingPropertiesOnDynamic
                    result = RazorDynamicObject.Create(original, allowMissing);
                }
                else if (allowMissing)
                {
                    result = RazorDynamicObject.Create(original, allowMissing);
                }
            }
            return(result);
        }
コード例 #7
0
        private Tuple <object, Type> CheckModel(object model)
        {
            if (model == null)
            {
                return(Tuple.Create((object)null, (Type)null));
            }
            Type modelType = (model == null) ? typeof(object) : model.GetType();

            bool isAnon = CompilerServicesUtility.IsAnonymousTypeRecursive(modelType);

            if (isAnon ||
                CompilerServicesUtility.IsDynamicType(modelType))
            {
                modelType = null;
                if (isAnon || Configuration.AllowMissingPropertiesOnDynamic)
                {
                    model = RazorDynamicObject.Create(model, Configuration.AllowMissingPropertiesOnDynamic);
                }
            }
            return(Tuple.Create(model, modelType));
        }
コード例 #8
0
 public override void Init(object model)
 {
     Model = new RazorDynamicObject(model);
 }