Ejemplo n.º 1
0
        private static void ThrowModelBindingException(bool sourceContent, bool modelContent, Type sourceType, Type modelType)
        {
            var msg = new StringBuilder();

            // prepare message
            msg.Append("Cannot bind source");
            if (sourceContent)
            {
                msg.Append(" content");
            }
            msg.Append(" type ");
            msg.Append(sourceType.FullName);
            msg.Append(" to model");
            if (modelContent)
            {
                msg.Append(" content");
            }
            msg.Append(" type ");
            msg.Append(modelType.FullName);
            msg.Append(".");

            // raise event, to give model factories a chance at reporting
            // the error with more details, and optionally request that
            // the application restarts.

            var args = new ModelBindingArgs(sourceType, modelType, msg);

            ModelBindingException?.Invoke(Instance, args);

            // TODO: with all of the tests I've done i don't think restarting the app here is required anymore,
            // when I don't have this code enabled and i get a model binding error and just refresh, it fixes itself.
            // We'll leave this for now though.
            if (args.Restart)
            {
                msg.Append(" The application is restarting now.");

                var context = HttpContext.Current;
                if (context == null)
                {
                    AppDomain.Unload(AppDomain.CurrentDomain);
                }
                else
                {
                    UmbracoApplication.Restart(new HttpContextWrapper(context));
                }
            }

            throw new ModelBindingException(msg.ToString());
        }
Ejemplo n.º 2
0
        private static void ThrowModelBindingException(bool sourceContent, bool modelContent, Type sourceType, Type modelType)
        {
            var msg = new StringBuilder();

            // prepare message
            msg.Append("Cannot bind source");
            if (sourceContent)
            {
                msg.Append(" content");
            }
            msg.Append(" type ");
            msg.Append(sourceType.FullName);
            msg.Append(" to model");
            if (modelContent)
            {
                msg.Append(" content");
            }
            msg.Append(" type ");
            msg.Append(modelType.FullName);
            msg.Append(".");

// raise event, to give model factories a chance at reporting
            // the error with more details, and optionally request that
            // the application restarts.

            var args = new ModelBindingArgs(sourceType, modelType, msg);

            ModelBindingException?.Invoke(Instance, args);

            if (args.Restart)
            {
                msg.Append(" The application is restarting now.");

                var context = HttpContext.Current;
                if (context == null)
                {
                    AppDomain.Unload(AppDomain.CurrentDomain);
                }
                else
                {
                    UmbracoApplication.Restart(new HttpContextWrapper(context));
                }
            }

            throw new ModelBindingException(msg.ToString());
        }