async Task DownloadImpl(String Base, String id, Boolean raw, HttpResponseBase Response, Action <ExpandoObject> setParams, String suffix, String token) { try { var url = $"/_attachment{Base}/{id}"; var ai = await _baseController.DownloadAttachment(url, setParams, suffix); if (ai == null) { throw new RequestModelException($"Attachment not found. (Id:{id})"); } if (!_baseController.IsTokenValid(Response, ai.Token, token)) { return; } if (raw) { Response.ContentType = "application/octet-stream"; } else { Response.ContentType = raw ? "application/octet-stream" : ai.Mime; String repName = ai.Name; if (String.IsNullOrEmpty(repName)) { repName = "Attachment"; } var cdh = new ContentDispositionHeaderValue("attachment") { FileNameStar = _baseController.Localize(repName) + Mime2Extension(ai.Mime) }; Response.Headers.Add("Content-Disposition", cdh.ToString()); } if (ai.Stream != null) { Response.BinaryWrite(ai.Stream); } } catch (Exception ex) { if (raw) { _baseController.WriteExceptionStatus(ex, Response); } else { _baseController.WriteHtmlException(ex, Response.Output); } } }
async Task Render(String pathInfo, RequestUrlKind kind) { /* * PARAMS: * 1. initial = [queryString, controller] * 2. real = [model.json, id, initial] */ // Ajax if (IsNotAjax()) { return; } try { Response.ContentType = "text/html"; ExpandoObject loadPrms = new ExpandoObject(); // query string loadPrms.Append(_baseController.CheckPeriod(Request.QueryString), toPascalCase: true); if (pathInfo.StartsWith("app/")) { // controller after query string SetUserTenantToParams(loadPrms); // without claims await _baseController.RenderApplicationKind(kind, pathInfo, loadPrms, Response.Output); } else { // controller after query string SetSqlQueryParams(loadPrms); await _baseController.RenderElementKind(kind, pathInfo, loadPrms, Response.Output); } } catch (Exception ex) { if (ex.Message.StartsWith("UI:", StringComparison.OrdinalIgnoreCase)) { var error = _baseController.Localize(ex.Message.Substring(3)); _baseController.WriteExceptionStatus(ex, Response); } else { _baseController.WriteHtmlException(ex, Response.Output); } } }
public async Task LoadData(HttpRequestBase request, HttpResponseBase response) { if (request.HttpMethod != "POST") { return; } response.ContentType = "application/json"; try { String json = null; using (var tr = new StreamReader(request.InputStream)) json = tr.ReadToEnd(); await _baseController.ReloadData(null, json, response.Output); } catch (Exception ex) { _baseController.WriteExceptionStatus(ex, response); } }
public async Task DefaultPOST(String pathInfo) { Guid apiGuid = Guid.NewGuid(); try { _logger.LogApi($"post: {pathInfo}", Request.UserHostAddress, apiGuid); var rm = await RequestModel.CreateFromApiUrl(_baseController.Host, "_api/" + pathInfo); var ac = rm.CurrentCommand; if (!ValidAllowAddress(ac)) { return; } Response.ContentType = "application/json"; Response.AddHeader("Access-Control-Allow-Origin", ac.AllowOriginForCheck); String json = null; Request.InputStream.Seek(0, SeekOrigin.Begin); // ensure using (var tr = new StreamReader(Request.InputStream)) { json = tr.ReadToEnd(); _logger.LogApi($"request: {json}", Request.UserHostAddress, apiGuid); } ExpandoObject dataToInvoke = JsonConvert.DeserializeObject <ExpandoObject>(json, new ExpandoObjectConverter()); if (dataToInvoke == null) { dataToInvoke = new ExpandoObject(); } if (!String.IsNullOrEmpty(ac.wrapper)) { ExpandoObject wrap = new ExpandoObject(); wrap.Set(ac.wrapper, dataToInvoke); dataToInvoke = wrap; } SetIdentityParams(dataToInvoke); await ExecuteCommand(ac, dataToInvoke, apiGuid); } catch (Exception ex) { if (ex.InnerException != null) { ex = ex.InnerException; } _logger.LogApiError(ex.Message, Request.UserHostAddress, apiGuid); _baseController.WriteExceptionStatus(ex, Response); } }
async Task Render(String pathInfo, RequestUrlKind kind) { // Ajax if (IsNotAjax()) { return; } try { Response.ContentType = "text/html"; ExpandoObject loadPrms = new ExpandoObject(); loadPrms.Append(_baseController.CheckPeriod(Request.QueryString), toPascalCase: true); if (pathInfo.StartsWith("app/")) { SetUserTenantToParams(loadPrms); // without claims await _baseController.RenderApplicationKind(kind, pathInfo, loadPrms, Response.Output); } else { SetSqlQueryParams(loadPrms); await _baseController.RenderElementKind(kind, pathInfo, loadPrms, Response.Output); } } catch (Exception ex) { if (ex.Message.StartsWith("UI:")) { var error = _baseController.Localize(ex.Message.Substring(3)); _baseController.WriteExceptionStatus(ex, Response); } else { _baseController.WriteHtmlException(ex, Response.Output); } } }
public async Task DefaultPOST(String pathInfo) { Guid apiGuid = Guid.NewGuid(); Int32 errorCode = 0; try { _logger.LogApi($"post: {pathInfo}", Request.UserHostAddress, apiGuid); var rm = await RequestModel.CreateFromApiUrl(_baseController.Host, "_api/" + pathInfo); var ac = rm.CurrentCommand; if (!ValidAllowAddress(ac)) { return; } if (!ValidHosts(ac)) { return; } if (!ac.IsPost()) { throw new RequestModelException($"Method 'post' is required for '{ac.command}' command"); } errorCode = ac.errorCode; if (ac.authorize && !User.Identity.IsAuthenticated) { _logger.LogApiError("Unauthorized", Request.UserHostAddress, apiGuid); Response.StatusCode = 401; return; } Response.ContentType = MimeTypes.Application.Json; Response.AddHeader("Access-Control-Allow-Origin", ac.AllowOriginForCheck); String json = null; Request.InputStream.Seek(0, SeekOrigin.Begin); // ensure using (var tr = new StreamReader(Request.InputStream)) { json = tr.ReadToEnd(); _logger.LogApi($"request: {json}", Request.UserHostAddress, apiGuid); } ExpandoObject dataToInvoke = JsonConvert.DeserializeObject <ExpandoObject>(json, new ExpandoObjectConverter()); if (dataToInvoke == null) { dataToInvoke = new ExpandoObject(); } if (!String.IsNullOrEmpty(ac.wrapper)) { ExpandoObject wrap = new ExpandoObject(); wrap.Set(ac.wrapper, dataToInvoke); dataToInvoke = wrap; } SetIdentityParams(dataToInvoke, ac.authorize); await ExecuteCommand(ac, dataToInvoke, apiGuid); await _baseController.ProcessDbEvents(ac); } catch (Exception ex) { if (ex.InnerException != null) { ex = ex.InnerException; } _logger.LogApiError(ex.Message, Request.UserHostAddress, apiGuid); _baseController.WriteExceptionStatus(ex, Response, errorCode); } }
protected void WriteExceptionStatus(Exception ex) { _baseController.WriteExceptionStatus(ex, Response); }
public void WriteExceptionStatus(Exception ex, HttpResponseBase response) { _baseController.WriteExceptionStatus(ex, response); }