Ejemplo n.º 1
0
        public override void Handle(HttpListenerContext context)
        {
            _logger.Info(context.Request.Url.LocalPath);

            if (context.Request.HasEntityBody)
            {
                User user = GetUser(context.Request);

                if (user != null)
                {
                    _service.Vote(user);
                }

                context.Response.Redirect("participants.html");

                List <User> users    = _service.ListAttendent();
                string      userList = GetParticipants(users);

                var html = GetView("participants.html");
                html = html.Replace("{{participants}}", userList);
                Render(html, context.Response);
            }
            else
            {
                string html = GetView("vote.html");
                Render(html, context.Response);
            }
        }
        public override void Handle(HttpListenerContext context)
        {
            Logger.Info(context.Request.Url.ToString());
            if (context.Request.HttpMethod.Equals("GET"))
            {
                string filePath    = GetPath("vote.html");
                string responseStr = GetResponse(filePath);
                Send(context, responseStr);
            }
            else if (context.Request.HttpMethod.Equals("POST"))
            {
                if (context.Request.Url.AbsolutePath == "/vote")
                {
                    StreamReader reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding);
                    string       s      = reader.ReadToEnd();

                    NameValueCollection query = HttpUtility.ParseQueryString(s);

                    if (query["attend"] == "on")
                    {
                        ParticipantsService.Vote(query["name"], true);
                    }
                    else
                    {
                        ParticipantsService.Vote(query["name"], false);
                    }

                    context.Response.Redirect("participants.html");
                }
                else
                {
                    context.Response.StatusCode = 404;
                }
            }
            else
            {
                context.Response.StatusCode = 501;
            }
        }