private IHttpResponse PrepareResponse(IActionResult actionResult) { string invocationResult = actionResult.Invoke(); if ((request.Cookies.ContainsCookie("-auth.cakes") && request.Cookies.GetCookie("-auth.cakes").Expires < DateTime.UtcNow) || (!request.Cookies.ContainsCookie("-auth.cakes"))) { layout = File.ReadAllText($"../" + Assembly.GetEntryAssembly().GetName().Name + "/Views/Layouts/_LoggedOutLayout.html"); } else { layout = File.ReadAllText($"../" + Assembly.GetEntryAssembly().GetName().Name + "/Views/Layouts/_LoggedInLayout.html"); } layout = layout.Replace("@Body", invocationResult); if (actionResult is IViewable) { var response = new HtmlResult(layout, HttpResponseStatusCode.Ok); if (this.request.Cookies.ContainsCookie("-auth.cakes")) { response.AddCookie(this.request.Cookies.GetCookie("-auth.cakes")); } return(response); } else if (actionResult is IRedirectable) { return(new SIS.WebServer.Results.RedirectResult((actionResult as IRedirectable).RedirectUrl)); } throw new InvalidOperationException(UnsupportedActionMessage); }
static void Main(string[] args) { var serverRoutingTable = new ServerRoutingTable(); var htmlResult = new HtmlResult("<h1>Hello World</h1>", HttpResponseStatusCode.Ok); htmlResult.AddCookie(new HttpCookie("lang", "en")); serverRoutingTable.Add(HttpRequestMethod.Get, "/", request => htmlResult); Server server = new Server(8001, serverRoutingTable); server.Run(); }
public IHttpResponse View([CallerMemberName] string view = null) { string controllerName = this.GetType().Name.Replace("Controller", string.Empty); string viewName = view; string viewContent = File.ReadAllText("Views/" + controllerName + "/" + viewName + ".html"); viewContent = this.ParseTemplate(viewContent); //Console.WriteLine($"Controller: {this.GetType().Name}, Method {view} "); HtmlResult htmlResult = new HtmlResult(viewContent, HttpResponseStatusCode.Ok); htmlResult.AddCookie(new HttpCookie("lang", "en")); return(htmlResult); }