internal WebPageHttpHandler(WebPage webPage, Lazy<WebPageRenderingBase> startPage) { if (webPage == null) { throw new ArgumentNullException("webPage"); } _webPage = webPage; _startPage = startPage; }
/// <summary> /// Executes the page code of the desired method /// and send it as JSON to the Http Response. /// </summary> /// <param name="WebPage"></param> public void JsonResponse(System.Web.WebPages.WebPage WebPage) { var data = Run(WebPage); WebPage.Response.ContentType = "application/json"; // JSON.NET Works Better: good datetime formatting as ISO-8601 and some bugfixes details. // IMPORTANT: Ideally, we must return dates as UTC, but currently datetimes from database are not detected as 'local' // so trying to return as 'utc' with DateTimezoneHandling.UTc results in the same time with a 'Z' in the end, that's an error. // Almost, using 'Local' will append the correct offset and browsers/javascript engines do the conversion correctly. WebPage.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(data, new Newtonsoft.Json.JsonSerializerSettings { DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local })); //Json.Write(jsondata, Response.Output); WebPage.Response.End(); }
/// <summary> /// Executes the page code of the desired method /// and returns an object with the result. /// That result must be piped to the response manually /// (or use JsonResponse). /// </summary> /// <param name="WebPage"></param> /// <returns></returns> public dynamic Run(System.Web.WebPages.WebPage WebPage) { this.WebPage = WebPage; dynamic result = null; // For all requests: // No cache WebPage.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); WebPage.Response.Cache.SetValidUntilExpires(false); WebPage.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); WebPage.Response.Cache.SetCacheability(HttpCacheability.NoCache); WebPage.Response.Cache.SetNoStore(); // No cookies WebPage.Response.Cookies.Clear(); try { switch (Request.HttpMethod.ToUpper()) { case "GET": result = Get(); break; case "POST": // By general, if everything goes fine, a 'post' must return // the http code '201:Created'. On any error will be replaced. // And can be explicitely replaced by the specific page on the // overrided 'Post()' method WebPage.Response.StatusCode = 201; result = Post(); break; case "PUT": result = Put(); break; case "DELETE": result = Delete(); break; case "OPTIONS": // Just let asp.net to response empty with the // custom headers for CORS that were set in the web.config break; default: throw new HttpException(405, String.Format("{0} is not allowed", Request.HttpMethod)); } } catch (ValidationException valEx) { result = new Dictionary <string, dynamic>(); result["errorMessage"] = LcRessources.ValidationSummaryTitle; result["errorSource"] = "validation"; var errors = new System.Collections.Hashtable(); if (!String.IsNullOrEmpty(valEx.ContainerName)) { errors[valEx.ContainerName + "." + valEx.ParamName] = valEx.Message; } errors[valEx.ParamName] = valEx.Message; result["errors"] = errors; WebPage.Response.StatusCode = 400; } catch (HttpException http) { result = new Dictionary <string, dynamic>(); WebPage.Response.StatusCode = http.GetHttpCode(); result["errorMessage"] = http.Message; if (!ModelState.IsValid) { result["errorSource"] = "validation"; result["errors"] = ModelState.Errors(); } if (WebPage.Response.StatusCode == 500) { if (ASP.LcHelpers.InDev) { result["exception"] = http; } else { LcLogger.LogAspnetError(http); } } } catch (Exception ex) { result = new Dictionary <string, dynamic>(); result["errorMessage"] = ex.Message; // Bad Format code for "Constraint" or Internal server error: WebPage.Response.StatusCode = ex is ConstraintException ? 400 : 500; if (ASP.LcHelpers.InDev) { result["exception"] = ex; } else { LcLogger.LogAspnetError(ex); } } return(result); }
public WebPageHttpHandler(WebPage webPage) : this(webPage, new Lazy<WebPageRenderingBase>(() => System.Web.WebPages.StartPage.GetStartPage(webPage, StartPageFileName, GetRegisteredExtensions()))) { }
public WebPageHttpHandler(WebPage webPage) { if (webPage == null) { throw new ArgumentNullException("webPage"); } _webPage = webPage; }
public WebPageHttpHandler(WebPage webPage) : this(webPage, new Lazy <WebPageRenderingBase>(() => System.Web.WebPages.StartPage.GetStartPage(webPage, StartPageFileName, GetRegisteredExtensions()))) { }