Esempio n. 1
0
        void proc()
        {
            JContext    jc      = JContext.Current;
            HttpContext context = jc.Context;

            // set a ajax request token
            jc.IsAjaxRequest = true;

            // get querystring
            string qs = context.Request.Params["querystring"];

            if (StringUtil.HasText(qs))
            {
                qs = qs.TrimStart('?');

                jc.QueryString.Add(StringUtil.DelimitedEquation2NVCollection("&", qs));
            }

            if (context.Request.UrlReferrer != null)
            {
                UrlMappingModule module = UrlMappingModule.Instance;
                if (module != null)
                {
                    UrlMappingItem mapping = null;
                    jc.QueryString.Add(module.GetMappedQueryString(context.Request.UrlReferrer.AbsolutePath, out mapping));

                    if (mapping != null)
                    {
                        NavigationInfo navi = new NavigationInfo();
                        navi.Set(mapping, UrlMappingModule.GetUrlRequested(context.Request.UrlReferrer.AbsolutePath));

                        jc.Navigation = navi;

                        // fire url matched event
                        module.OnUrlMatched();
                    }
                }
            }

            // set view data
            UrlMappingModule.SetViewData();

            string classId        = context.Request.Params[CLASS_ID_PARAM];
            string methodName     = context.Request.Params[METHOD_NAME_PARAM];
            string methodJsonArgs = context.Request.Params[METHOD_ARGS_PARAM];
            string jsonp          = context.Request.Params[JSONP];

            object result;
            int    cacheMinutes = -1;

            if (string.IsNullOrEmpty(classId) || string.IsNullOrEmpty(methodName))
            {
                result = "null";
            }
            else
            {
                AjaxConfiguration config = AjaxConfiguration.GetConfig();

                AjaxMethod m = null;

                try
                {
                    string id = jc.Navigation.Id;
                    if (id.Contains(":"))
                    {
                        id = id.Substring(id.IndexOf(":") + 1);
                    }

                    AjaxClass c = config.FindClass(classId, id);

                    m = config.FindMethod(c, methodName);

                    if (string.Equals("Post", m.AjaxType, StringComparison.InvariantCultureIgnoreCase))
                    {
                        cacheMinutes = -1;
                    }
                    else if (StringUtil.HasText(m.CacheTest))
                    {
                        cacheMinutes = methodJsonArgs.Equals(m.CacheTest) ? cacheMinutes : -1;
                    }

                    // before execute
                    BeforeExecuteEventArgs e = new BeforeExecuteEventArgs()
                    {
                        JContext = jc, TypeName = c.Key, MethodName = m.MethodName
                    };
                    OnBeforeExecute(e);
                    if (e.PreventDefault)
                    {
                        result = e.ReturnValue;
                        goto response;
                    }

                    if (c.Type != null)
                    {
                        result = m.Invoke(c.Type, methodJsonArgs);
                    }
                    else
                    {
                        result = m.Invoke(c.TypeString, methodJsonArgs);
                    }
                }
                catch (Exception ex)
                {
                    LogManager.GetLogger <AjaxController>().Error("ajax handler error." + ExceptionUtil.WriteException(ex));

                    AjaxServerException ajaxEx = null;
                    if (m != null)
                    {
                        ajaxEx = m.Exception;
                    }

                    if (ajaxEx != null)
                    {
                        result = ajaxEx.ToJson();
                    }
                    else
                    {
                        result = null;
                    }
                }
            }

            goto response;

response:

            OnAfterExecute(result);

            ResponseUtil.OutputJson(context.Response, result, cacheMinutes, jsonp);
            ContentType = context.Response.ContentType;
        }
        void proc()
        {
            JContext jc = JContext.Current;
            HttpContext context = jc.Context;

            // set a ajax request token
            jc.IsAjaxRequest = true;

            // get querystring
            string qs = context.Request.Params["querystring"];
            if (StringUtil.HasText(qs))
            {
                qs = qs.TrimStart('?');

                jc.QueryString.Add(StringUtil.DelimitedEquation2NVCollection("&", qs));
            }

            if (context.Request.UrlReferrer != null)
            {
                UrlMappingModule module = UrlMappingModule.Instance;
                if (module != null)
                {
                    UrlMappingItem mapping = null;
                    jc.QueryString.Add(module.GetMappedQueryString(context.Request.UrlReferrer.AbsolutePath, out mapping));

                    if (mapping != null)
                    {
                        NavigationInfo navi = new NavigationInfo();
                        navi.Set(mapping, UrlMappingModule.GetUrlRequested(context.Request.UrlReferrer.AbsolutePath));

                        jc.Navigation = navi;

                        // fire url matched event
                        module.OnUrlMatched();
                    }
                }
            }

            // set view data 
            UrlMappingModule.SetViewData();

            string classId = context.Request.Params[CLASS_ID_PARAM];
            string methodName = context.Request.Params[METHOD_NAME_PARAM];
            string methodJsonArgs = context.Request.Params[METHOD_ARGS_PARAM];
            string jsonp = context.Request.Params[JSONP];

            object result;
            int cacheMinutes = -1;

            if (string.IsNullOrEmpty(classId) || string.IsNullOrEmpty(methodName))
            {
                result = "null";
            }
            else
            {
                AjaxConfiguration config = AjaxConfiguration.GetConfig();

                AjaxMethod m = null;

                try
                {
                    string id = jc.Navigation.Id;
                    if (id.Contains(":"))
                        id = id.Substring(id.IndexOf(":") + 1);

                    AjaxClass c = config.FindClass(classId, id);

                    m = config.FindMethod(c, methodName);

                    if (string.Equals("Post", m.AjaxType, StringComparison.InvariantCultureIgnoreCase))
                        cacheMinutes = -1;
                    else if (StringUtil.HasText(m.CacheTest))
                        cacheMinutes = methodJsonArgs.Equals(m.CacheTest) ? cacheMinutes : -1;

                    // before execute
                    BeforeExecuteEventArgs e = new BeforeExecuteEventArgs() { JContext = jc, TypeName = c.Key, MethodName = m.MethodName };
                    OnBeforeExecute(e);
                    if (e.PreventDefault)
                    {
                        result = e.ReturnValue;
                        goto response;
                    }

                    if (c.Type != null)
                        result = m.Invoke(c.Type, methodJsonArgs);
                    else
                        result = m.Invoke(c.TypeString, methodJsonArgs);
                }
                catch (Exception ex)
                {
                    LogManager.GetLogger<AjaxController>().Error("ajax handler error." + ExceptionUtil.WriteException(ex));

                    AjaxServerException ajaxEx = null;
                    if (m != null)
                        ajaxEx = m.Exception;

                    if (ajaxEx != null)
                        result = ajaxEx.ToJson();
                    else
                        result = null;
                }
            }

            goto response;

        response:

            OnAfterExecute(result);

            ResponseUtil.OutputJson(context.Response, result, cacheMinutes, jsonp);
            ContentType = context.Response.ContentType;
        }