Пример #1
0
        private static HttpResponseException ReportToLogAndThrow(HttpRequestMessage request, HttpStatusCode code, Exception e, string msg, Func <string, HttpControllerDescriptor, HttpControllerDescriptor> wrapLog)
        {
            var helpText  = ErrorHelp.HelpText(e);
            var exception = new Exception(msg + helpText, e);

            DotNetNuke.Services.Exceptions.Exceptions.LogException(exception);
            wrapLog("error", null);
            return(new HttpResponseException(request.CreateErrorResponse(code, exception.Message, e)));
        }
Пример #2
0
        internal static HttpResponseException ReportToLogAndThrow <T>(HttpRequestMessage request, HttpStatusCode code, Exception e, string msg, Func <string, T, T> wrapLog)
        {
            var helpText  = ErrorHelp.HelpText(e);
            var exception = new Exception(msg + helpText, e);

            DotNetNuke.Services.Exceptions.Exceptions.LogException(exception);
            wrapLog("error", default);
            return(new HttpResponseException(request.CreateErrorResponse(code, exception.Message, e)));
        }
Пример #3
0
        public HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            // Log this lookup and add to history for insights
            var log = new Log("Sxc.Http", null, request?.RequestUri?.AbsoluteUri);

            AddToInsightsHistory(request?.RequestUri?.AbsoluteUri, log);

            var wrapLog = log.Call <HttpControllerDescriptor>();

            if (!HandleRequestWithThisController(request))
            {
                return(wrapLog("upstream", PreviousSelector.SelectController(request)));
            }

            var routeData          = request.GetRouteData();
            var controllerTypeName = routeData.Values[RouteParts.ControllerKey] + "Controller";

            // Handle the app-api queries
            try
            {
                var appFolder = Route.AppPathOrNull(routeData);

                if (appFolder == null)
                {
                    log.Add("no folder found in url, will auto-detect");
                    var sexy = Helpers.GetCmsBlock(request, false, log);
                    appFolder = sexy.App.Folder;
                }

                log.Add($"App Folder: {appFolder}");

                // new for 2sxc 9.34 #1651
                var edition = "";
                if (routeData.Values.ContainsKey(RouteParts.EditionKey))
                {
                    edition = routeData.Values[RouteParts.EditionKey].ToString();
                }
                if (!string.IsNullOrEmpty(edition))
                {
                    edition += "/";
                }

                log.Add($"Edition: {edition}");

                var controllerFolder = Path.Combine(DnnMapAppToInstance.AppBasePath(), appFolder,
                                                    edition + "api/");

                controllerFolder = controllerFolder.Replace("\\", @"/");
                log.Add($"Controller Folder: {controllerFolder}");

                var controllerPath = Path.Combine(controllerFolder + controllerTypeName + ".cs");
                log.Add($"Controller Path: {controllerPath}");

                // note: this may look like something you could optimize/cache the result, but that's a bad idea
                // because when the file changes, the type-object will be different, so please don't optimize :)
                if (File.Exists(HostingEnvironment.MapPath(controllerPath)))
                {
                    var assembly = BuildManager.GetCompiledAssembly(controllerPath);
                    var type     = assembly.GetType(controllerTypeName, true, true);

                    // help with path resolution for compilers running inside the created controller
                    request?.Properties.Add(CodeCompiler.SharedCodeRootPathKeyInCache, controllerFolder);

                    var descriptor = new HttpControllerDescriptor(_config, type.Name, type);
                    return(wrapLog("ok", descriptor));
                }

                log.Add("path not found");
            }
            catch (Exception e)
            {
                var apiErrPrefix = "2sxc Api Controller Finder: " +
                                   "Error selecting / compiling an API controller. " +
                                   "Check event-log, code and inner exception. ";
                var helpText  = ErrorHelp.HelpText(e);
                var exception = new Exception(apiErrPrefix + helpText, e);
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exception);
                wrapLog("error", null);
                throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception.Message, e));
            }

            wrapLog("error", null);
            throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound, "2sxc Api Controller Finder: Controller " + controllerTypeName + " not found in app."));
        }