示例#1
0
        private void Httpsv_OnPost(object sender, HttpRequestEventArgs e)
        {
            MAction emm       = new MAction();
            string  logResult = string.Empty;

            try
            {
                byte[] bf     = new byte[e.Request.ContentLength64];
                string result = Encoding.UTF8.GetString(GetResult(e, bf));   //获取http请求内容
                logResult = result;
                if (dic.ContainsKey(e.Request.RawUrl))
                {
                    emm = dic[e.Request.RawUrl];
                    object   obj;
                    object[] objs = new object[emm.parameterInfo.Length];
                    if (emm.parameterInfo.Length > 1)
                    {
                        throw new Exception("POST方法只允许有一个参数");
                    }
                    else if (emm.parameterInfo.Length == 0)
                    {
                        obj = new object();
                    }
                    else
                    {
                        obj     = Newtonsoft.Json.JsonConvert.DeserializeObject(result, emm.parameterInfo[0].ParameterType);
                        objs[0] = obj;
                    }
                    var action = container.Resolve(emm.controllerType);
                    var data   = emm.action.Invoke(action, objs);
                    e.Response.ContentType = "application/json";
                    if (data == null)
                    {
                        e.Response.StatusCode = 204;
                    }
                    else
                    {
                        byte[] buffer = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(data));
                        e.Response.WriteContent(buffer);
                    }
                }
                else
                {
                    e.Response.StatusCode = 404;
                }
            }
            catch (Exception ex)
            {
                if (emm.exceptionFilter == null)
                {
                    byte[] buffer = Encoding.UTF8.GetBytes(ex.Message + ex.StackTrace);
                    e.Response.WriteContent(buffer);
                    e.Response.StatusCode = 500;
                }
                else
                {
                    if (emm.exceptionFilter != null)
                    {
                        if (ex.InnerException != null)
                        {
                            emm.exceptionFilter.OnException(ex.InnerException, e);
                        }
                        else
                        {
                            emm.exceptionFilter.OnException(ex, e);
                        }
                    }
                }
            }
        }
示例#2
0
        private void Httpsv_OnGet(object sender, HttpRequestEventArgs e)
        {
            var     req    = e.Request;
            var     res    = e.Response;
            string  rawUrl = req.RawUrl;
            MAction emm    = new MAction();

            if (Path.GetExtension(req.RawUrl).Length > 0)
            {
                string path        = AppDomain.CurrentDomain.BaseDirectory + req.RawUrl;
                string contentType = MimeMapping.GetMimeMapping(path);
                byte[] buffer      = File.ReadAllBytes(path);
                res.ContentType = contentType;
                res.WriteContent(buffer);
            }
            else
            {
                int index = rawUrl.IndexOf('?');
                if (index > 0)
                {
                    rawUrl = rawUrl.Substring(0, index);
                }
                string result = string.Empty;
                try
                {
                    if (dic.ContainsKey(rawUrl))
                    {
                        emm = dic[rawUrl];
                        var      action = container.Resolve(emm.controllerType);
                        object[] objs   = new object[emm.parameterInfo.Length];
                        int      i      = 0;
                        foreach (var item in emm.parameterInfo)
                        {
                            if (item.ParameterType == typeof(int))
                            {
                                objs[i] = int.Parse(e.Request.QueryString.Get(item.Name));
                            }
                            else
                            {
                                objs[i] = e.Request.QueryString.Get(item.Name);
                            }
                            i = i + 1;
                        }
                        var data = emm.action.Invoke(action, objs);
                        e.Response.ContentType = "application/json";
                        if (data == null)
                        {
                            e.Response.StatusCode = 204;
                        }
                        else
                        {
                            byte[] buffer = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(data));
                            e.Response.WriteContent(buffer);
                        }
                    }
                    else
                    {
                        e.Response.StatusCode = 404;
                    }
                }
                catch (Exception ex)
                {
                    if (emm.exceptionFilter == null)
                    {
                        byte[] buffer = Encoding.UTF8.GetBytes(ex.Message + ex.StackTrace);
                        e.Response.WriteContent(buffer);
                        e.Response.StatusCode = 500;
                    }
                    else
                    {
                        if (emm.exceptionFilter != null)
                        {
                            if (ex.InnerException != null)
                            {
                                emm.exceptionFilter.OnException(ex.InnerException, e);
                            }
                            else
                            {
                                emm.exceptionFilter.OnException(ex, e);
                            }
                        }
                    }
                }
            }
        }