protected override void Initialize(HttpControllerContext controllerContext) { base.Initialize(controllerContext); Block = DnnGetBlock.GetCmsBlock(Request, true, Log); }
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 block = DnnGetBlock.GetCmsBlock(request, false, log); appFolder = block.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 tenant = Factory.Resolve <ITenant>(); var controllerFolder = Path.Combine(tenant.AppsRoot, 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.")); }