public ActionResult AddWidget(int layerId, string widgetType, string zone, string returnUrl)
        {
            if (!IsAuthorizedToManageWidgets())
            {
                return(new HttpUnauthorizedResult());
            }

            WidgetPart widgetPart = Services.ContentManager.New <WidgetPart>(widgetType);

            if (widgetPart == null)
            {
                return(HttpNotFound());
            }
            try {
                int widgetPosition = _widgetsService.GetWidgets().Count(widget => widget.Zone == widgetPart.Zone) + 1;
                widgetPart.Position  = widgetPosition.ToString(CultureInfo.InvariantCulture);
                widgetPart.Zone      = zone;
                widgetPart.LayerPart = _widgetsService.GetLayer(layerId);
                var model = Services.ContentManager.BuildEditor(widgetPart);

                return(View(model));
            }
            catch (Exception exception) {
                Logger.Error(T("Creating widget failed: {0}", exception.Message).Text);
                Services.Notifier.Error(T("Creating widget failed: {0}", exception.Message));
                return(this.RedirectLocal(returnUrl, () => RedirectToAction("Index")));
            }
        }
        public ActionResult AddWidget(int layerId, string widgetType, string zone, string returnUrl)
        {
            if (!Services.Authorizer.Authorize(Permissions.ManageWidgets, T(NotAuthorizedManageWidgetsLabel)))
            {
                return(new HttpUnauthorizedResult());
            }

            try {
                WidgetPart widgetPart = Services.ContentManager.New <WidgetPart>(widgetType);
                if (widgetPart == null)
                {
                    return(HttpNotFound());
                }

                int widgetPosition = _widgetsService.GetWidgets().Where(widget => widget.Zone == widgetPart.Zone).Count() + 1;
                widgetPart.Position  = widgetPosition.ToString();
                widgetPart.Zone      = zone;
                widgetPart.LayerPart = _widgetsService.GetLayer(layerId);
                dynamic model = Services.ContentManager.BuildEditor(widgetPart);
                // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
                return(View((object)model));
            }
            catch (Exception exception) {
                this.Error(exception, T("Creating widget failed: {0}", exception.Message), Logger, Services.Notifier);
                return(this.RedirectLocal(returnUrl, () => RedirectToAction("Index")));
            }
        }