Exemplo n.º 1
0
 public RequestAnalysisResult GetRequestAnalysis(RequestAnalysisContext analysisContext)
 {
     foreach (var provider in Providers)
     {
         RequestAnalysisResult analysisResult = provider.GetRequestAnalysis(analysisContext);
         if (analysisResult != null)
         {
             return(analysisResult);
         }
     }
     return(null);
 }
Exemplo n.º 2
0
 protected void ExcuteCore(RequestAnalysisContext analysisContext, IRequestAnalysisProvider provider)
 {
     try
     {
         RequestAnalysisResult analysisResult = provider.GetRequestAnalysis(analysisContext);
         if (!string.IsNullOrEmpty(analysisResult?.Mode))
         {
             OutputResult(analysisResult);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine($"Url: {HttpContext.Current.Request.Url}");
         Debug.WriteLine(ex.Message);
     }
 }
Exemplo n.º 3
0
        protected virtual void OutputResult(RequestAnalysisResult requestAnalysis)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("=================================================================================");
            sb.AppendLine($"Time: {DateTime.Now.ToLocalTime()}");
            sb.AppendLine($"Url: {requestAnalysis.Url}");
            sb.AppendLine($"Mode: {requestAnalysis.Mode}");
            sb.AppendLine($"Controller: {requestAnalysis.ControllerName}");
            sb.AppendLine($"Action: {requestAnalysis.ActionName}");
            sb.AppendLine($"FilePath: {requestAnalysis.FilePath}");
            sb.AppendLine($"Parameters: {string.Join(",", requestAnalysis.Parameters)}");
            sb.AppendLine($"SupportedHttpMethods: {string.Join(",", requestAnalysis.SupportedHttpMethods)}");
            sb.AppendLine("=================================================================================");

            Output(sb.ToString());
        }
Exemplo n.º 4
0
        public override RequestAnalysisResult GetRequestAnalysis(RequestAnalysisContext analysisContext)
        {
            HttpRequestMessage request           = analysisContext.RequestMessage;
            HttpConfiguration  httpConfiguration = request.GetConfiguration();
            IHttpRouteData     httpRouteData     = httpConfiguration.Routes.GetRouteData(request);

            request.SetRouteData(httpRouteData);
            //IAssembliesResolver assembliesResolver= httpConfiguration.Services.GetAssembliesResolver();
            //IHttpControllerTypeResolver controllerTypeResolver = httpConfiguration.Services.GetHttpControllerTypeResolver();
            //ICollection<Type> controllerTypes= controllerTypeResolver.GetControllerTypes(assembliesResolver);
            IHttpControllerSelector  controllerSelector   = httpConfiguration.Services.GetHttpControllerSelector();
            HttpControllerDescriptor controllerDescriptor = controllerSelector.SelectController(request);

            HttpControllerContext controllerContext = new HttpControllerContext(httpConfiguration, httpRouteData, request);

            controllerContext.ControllerDescriptor = controllerDescriptor;
            IHttpActionSelector  actionSelector   = httpConfiguration.Services.GetActionSelector();
            HttpActionDescriptor actionDescriptor = actionSelector.SelectAction(controllerContext);


            RequestAnalysisResult analysisResult = new RequestAnalysisResult();

            analysisResult.Url = request.RequestUri.ToString();
            analysisResult.SupportedHttpMethods = actionDescriptor.SupportedHttpMethods.Select(method => method.Method).ToArray();
            analysisResult.Parameters           = actionDescriptor.GetParameters().Select(parameter => parameter.ParameterName).ToArray();
            analysisResult.ActionName           = actionDescriptor.ActionName;
            analysisResult.ControllerName       = actionDescriptor.ControllerDescriptor.ControllerName;
            analysisResult.Values     = httpRouteData.Values;
            analysisResult.DataTokens = httpRouteData.Route.DataTokens;
            analysisResult.Mode       = Mode;

            string path = new DirectoryInfo(string.Format(@"{0}{1}", AppDomain.CurrentDomain.BaseDirectory, LookupPrefix)).FullName;

            analysisResult.FilePath = LookupFilePath(path, analysisResult.ControllerName);

            return(analysisResult);
        }
        public override RequestAnalysisResult GetRequestAnalysis(RequestAnalysisContext analysisContext)
        {
            RouteData             routeData      = analysisContext.RouteData;
            RequestAnalysisResult analysisResult = new RequestAnalysisResult();
            string controllerName = routeData.Values.TryGetValue("controller", out object controller) ? controller.ToString() : null;

            analysisResult.ActionName = routeData.Values.TryGetValue("action", out object action) ? action.ToString() : null;

            if (!string.IsNullOrEmpty(controllerName) && !string.IsNullOrEmpty(analysisResult.ActionName))
            {
                IControllerFactory   controllerFactory    = ControllerBuilder.Current.GetControllerFactory();
                RequestContext       requestContext       = new RequestContext(new HttpContextWrapper(HttpContext.Current), routeData);
                IController          controllerInstance   = controllerFactory.CreateController(requestContext, controllerName);
                ControllerDescriptor controllerDescriptor = new ReflectedControllerDescriptor(controllerInstance.GetType());
                ControllerContext    controllerContext    = new ControllerContext(requestContext, controllerInstance as ControllerBase);
                ActionDescriptor     actionDescriptor     = controllerDescriptor.FindAction(controllerContext, analysisResult.ActionName);

                analysisResult.Url            = HttpContext.Current.Request.Url.ToString();
                analysisResult.Mode           = Mode;
                analysisResult.Values         = routeData.Values;
                analysisResult.DataTokens     = routeData.DataTokens;
                analysisResult.ControllerName = controllerDescriptor.ControllerType.FullName;
                analysisResult.Parameters     = actionDescriptor.GetParameters().Select(parameter => parameter.ParameterName).ToArray();

                analysisResult.SupportedHttpMethods = actionDescriptor.GetCustomAttributes(typeof(ActionMethodSelectorAttribute), true)
                                                      .Select(methodAttribute => methodAttribute.GetType().Name.Replace("Attribute", "").Replace("Http", "")).ToArray();

                analysisResult.SupportedHttpMethods = analysisResult.SupportedHttpMethods.Count() == 0
                    ? new string[1] {
                    "All"
                } : analysisResult.SupportedHttpMethods;

                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, LookupPrefix);
                analysisResult.FilePath = LookupFilePath(path, analysisResult.ControllerName);
            }
            return(analysisResult);
        }