zoukankan      html  css  js  c++  java
  • 执行指定的操作的一般处理程序

    using Newtonsoft.Json;
    using System;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.SessionState;
    
    namespace Menhub {
        public abstract class PointedMethodHttpHandler<T> : IHttpHandler, IRequiresSessionState {
            public bool IsReusable {
                get { return false; }
            }
    
            static Lazy<MethodInfo[]> methods = new Lazy<MethodInfo[]>(() => typeof(T).GetMethods());
            protected abstract string ServerMethod { get; }
            protected virtual object Instance {
                get {
                    return this;
                }
            }
            public void ProcessRequest(HttpContext context) {
                var methodName = this.GetString(ServerMethod, @"[a-zA-Z][a-zA-Z0-9]+");
                var method = methods.Value.FirstOrDefault(m => m.Name.ToLower() == methodName.ToLower());
                if (method == null)
                    throw this.recordError("if (method == null)");
                object result;
                try {
                    result = method.Invoke(this.Instance, null);
                } catch (Exception e) {
                    throw this.recordError(e.GetinnerMessage());
                }
                HttpContext.Current.Response.ContentType = "application/json";
                HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result));
            }
            protected bool GetString(string key, string patten, out string value) {
                value = HttpContext.Current.Request[key];
                return Regex.IsMatch((value ?? string.Empty).Trim(), patten);
            }
            protected string GetString(string key) {
                return (HttpContext.Current.Request[key] ?? string.Empty).Trim();
            }
            protected string GetString(string key, bool required) {
                var result = (HttpContext.Current.Request[key] ?? string.Empty).Trim();
                if (result.Length == 0 && required) throw this.recordError("key: {0} is required.", key);
                return result;
            }
            /// <summary>
            /// return a required value. pass patten as null or "" if the value is only required.
            /// </summary>
            protected string GetString(string key, string patten) {
                var value = HttpContext.Current.Request[key];
                if ((value ?? string.Empty).Trim().Length == 0 || ((patten ?? string.Empty).Length > 0 && !Regex.IsMatch(value, patten)))
                    throw this.recordError("key: {0} is required or doesn't match the patten: {1}.", key, patten);
                return value.Trim();
            }
            protected virtual Exception recordError(string format, params object[] errors) {
                var request = HttpContext.Current.Request;
                var f = File.AppendText(request.MapPath("error-log.txt"));
                f.WriteLine("-------------{0}:{1}", DateTime.Now, request.Path);
                var all = request.Params;
                foreach (var key in all.AllKeys)
                    f.WriteLine("key:{0}
    	value:{1}", key, all[key]);
                var errorsText = string.Format(format, errors);
                f.WriteLine(errorsText);
                f.Write("--------------------------------------");
                f.Flush();
                f.Close();
                return new Exception(errorsText);
            }
        }
        public static class ExceptionExtensoin {
            public static string GetinnerMessage(this Exception e) {
                return e == null ? string.Empty : e.InnerException == null ? e.Message : e.InnerException.GetinnerMessage();
            }
        }
    }
  • 相关阅读:
    应用开发框架之——业务规则脚本化
    tms脚本演示代码之一
    根据.DFM文件动态生成窗体以及在之前先必须注册窗体中使用到的类
    界面/业务规则脚本化
    delphi 脚本引擎比较
    html5 datalist 选中option选项后的触发事件
    Laravel 5.6 模型关联 user 表后查询 user 表数据只能获取第一条数据,不知道怎么获取第二条...
    小技巧两个感叹号(两个!)连用
    Bootstrap 字体图标(Glyphicons)
    使用withCount后再使用select设置查询的字段。就找不到withCount的数据了
  • 原文地址:https://www.cnblogs.com/ly45/p/4510609.html
Copyright © 2011-2022 走看看