예제 #1
0
        /// <summary>
        /// 初始化操作
        /// </summary>
        /// <param name="controllerContext"></param>
        protected void Initialize(HttpControllerContext controllerContext)
        {
            Context   = controllerContext;
            Request   = controllerContext.Request;
            RouteData = controllerContext.RouteData;

            ActionName = RouteData.Values["action"] as string;
            Method     = Request.Method;

            var ctx = Request.Properties["MS_HttpContext"] as HttpContextWrapper;

            if (ctx != null)
            {
                RequestParameters = new RequestValues(ctx.Request.Unvalidated);
                ctx.Request.InputStream.Position = 0;
                RequestParameters.FormBodyBytes  = ctx.Request.BinaryRead(ctx.Request.ContentLength); //读取body数据
            }
            else
            {
                RequestParameters = new RequestValues(null);
                //TODO:如何取值,可以从Request出发
            }
            var mediaType = Request.Content?.Headers?.ContentType?.MediaType;
            var isJson    = mediaType.Equals("application/json", StringComparison.OrdinalIgnoreCase);

            if (isJson)
            {
                var charSet    = Request.Content?.Headers?.ContentType?.CharSet;
                var encoding   = charSet == null ? Encoding.UTF8 : Encoding.GetEncoding(charSet);
                var jsonString = encoding.GetString(RequestParameters.FormBodyBytes);
                RequestParameters.FormBody = new JsonFormBody(jsonString);
            }

            ReadRouteData(RequestParameters);//获取路由数据
        }
예제 #2
0
        /// <summary>
        /// 读取路由参数,保存到请求变量中
        /// </summary>
        /// <param name="values">请求变量</param>
        private void ReadRouteData(RequestValues values)
        {
            var nv = new NameValueCollection();

            foreach (var item in RouteData.Values)
            {
                nv.Add(item.Key, item.Value + "");
            }
            values.RouteDatas = nv;
        }
예제 #3
0
        public void TryParse(RequestValues values, out object value)
        {
            var paramValue = values[Name];

            if (paramValue != null)
            {
                Try(paramValue, out value);
            }
            else
            {
                Try(values, out value);
            }
        }
예제 #4
0
 protected override void Try(RequestValues args, out object value)
 {
     value = null;
 }
예제 #5
0
 protected abstract void Try(RequestValues args, out object value);