void ApplyResponseHeaders(HttpResponse response, MvcContext context) { foreach(var header in context.ResponseHeaders) { response.Headers[header.Key] = header.Value; } foreach(var cookie in context.ResponseCookies) { response.Cookies[cookie.Key] = cookie.Value; } }
static bool ServeMvcResponse(HttpListenerResponse httpResponse, HttpResponse mvcResponse) { var status = mvcResponse.Status; httpResponse.StatusCode = status; httpResponse.ContentType = mvcResponse.ContentType; httpResponse.RedirectLocation = mvcResponse.RedirectUrl; // headers if (mvcResponse.Headers.Count > 0) { foreach (var header in mvcResponse.Headers) { httpResponse.AppendHeader(header.Key as String, header.Value as String); } } // cookies var cookies = mvcResponse.Cookies; if(cookies != null && cookies.Count > 0) { foreach (var cookie in cookies.Values) { var cookieHeader = GetHeaderValue(cookie); httpResponse.Headers.Add("Set-Cookie", cookieHeader); } } // body var body = mvcResponse.Body; if (body != null) { byte[] buffer; var byteArrayBody = body as byte[]; if (byteArrayBody != null) { buffer = byteArrayBody; } else { // si no son bytes tratarlo siempre como un string. buffer = Encoding.UTF8.GetBytes(body.ToString()); } httpResponse.ContentLength64 = buffer.LongLength; httpResponse.OutputStream.Write(buffer, 0, buffer.Length); } return true; }