/// <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);//获取路由数据 }
/// <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; }
public void TryParse(RequestValues values, out object value) { var paramValue = values[Name]; if (paramValue != null) { Try(paramValue, out value); } else { Try(values, out value); } }
protected override void Try(RequestValues args, out object value) { value = null; }
protected abstract void Try(RequestValues args, out object value);