示例#1
0
        /// <summary>
        /// Generates an AJAX JavaScript proxy for a given controller.
        /// </summary>
        /// <param name="context">The context of the current request</param>
        /// <param name="proxyName">Name of the javascript proxy object</param>
        /// <param name="controller">Controller which will be target of the proxy</param>
        /// <param name="area">area which the controller belongs to</param>
        public string GenerateJSProxy(IEngineContext context, string proxyName, string area, string controller)
        {
            var cacheKey = (area + "|" + controller).ToLower(CultureInfo.InvariantCulture);
            var result   = (String)ajaxProxyCache[cacheKey];

            if (result != null)
            {
                return(WrapProxyIfNeeded(result));
            }

            logger.Debug("Ajax Proxy for area: '{0}', controller: '{1}' was not found. Generating a new one", area, controller);

            var controllerType = controllerTree.GetController(area, controller);

            if (controllerType == null)
            {
                logger.Fatal("Controller not found for the area and controller specified");
                throw new MonoRailException("Controller not found with Area: '{0}', Name: '{1}'", area, controller);
            }

            var baseUrl = GetBaseUrl(area, controller, context);

            // TODO: develop a smarter function generation, inspecting the return
            // value of the action and generating a proxy that does the same.
            // also, consider a proxy pattern for the Ajax.Updater.
            var metaDescriptor = controllerDescriptorBuilder.BuildDescriptor(controllerType);

            var functions = new StringBuilder();

            functions.AppendLine("{");

            for (var i = 0; i < metaDescriptor.AjaxActions.Count; i++)
            {
                var ajaxActionMethod = metaDescriptor.AjaxActions[i];
                var ajaxActionAtt    = GetSingleAttribute <AjaxActionAttribute>(ajaxActionMethod, true);

                var httpRequestMethod = GetHTTPRequestMethod(ajaxActionMethod);
                var extension         = GetURLExtension(context);
                var url          = baseUrl + ajaxActionMethod.Name + extension;
                var functionName = ToCamelCase(ajaxActionAtt.Name ?? ajaxActionMethod.Name);

                var scriptFunctionParameters = GetScriptFunctionParameters(ajaxActionMethod.GetParameters());

                functions.Append(
                    GenerateJavascriptFunction(url, functionName, httpRequestMethod, scriptFunctionParameters)
                    );

                // js functions are seperated by a comma
                if (metaDescriptor.AjaxActions.Count > 0 && i < metaDescriptor.AjaxActions.Count - 1)
                {
                    functions.AppendLine(",");
                }
            }

            functions.AppendLine("};");
            ajaxProxyCache[cacheKey] = "var " + proxyName + " =" + Environment.NewLine + functions;

            return(GenerateJSProxy(context, proxyName, area, controller));
        }
示例#2
0
        public IController CreateController(string area, string controller)
        {
            var implType = controllerTree.GetController(area, controller);

            if (implType == null)
            {
                throw new ControllerNotFoundException("Controller not found on the Windsor container instance. " +
                                                      "Have you registered it? Name: '" + controller + "' area: '" + area + "'");
            }

            return(CreateController(implType));
        }