How to implement proper HTTP error handling in .NET MVC 2?

Here’s one technique you could use. Define an ErrorsController which will serve the error pages: public class ErrorsController : Controller { public ActionResult Http404() { Response.StatusCode = 404; return Content(“404”, “text/plain”); } public ActionResult Http500() { Response.StatusCode = 500; return Content(“500”, “text/plain”); } public ActionResult Http403() { Response.StatusCode = 403; return Content(“403”, “text/plain”); } } … Read more

Django return HttpResponseRedirect to an url with a parameter

This should not be complicated. The argument to HttpResponseRedirect is simply a string, so the normal rules for building up a string apply here. However, I don’t think you want the theclass variable in there, as that is a ClassRoom object, not a string. You presumably want the classname instead. adamk has given you the … Read more

Execute code in Django after response has been sent to the client

The method I am going for at the moment uses a subclass of HttpResponse: from django.template import loader from django.http import HttpResponse # use custom response class to override HttpResponse.close() class LogSuccessResponse(HttpResponse): def close(self): super(LogSuccessResponse, self).close() # do whatever you want, this is the last codepoint in request handling if self.status_code == 200: print(‘HttpResponse successful: … Read more

Writing MemoryStream to Response Object

I had the same problem and the only solution that worked was: Response.Clear(); Response.ContentType = “Application/msword”; Response.AddHeader(“Content-Disposition”, “attachment; filename=myfile.docx”); Response.BinaryWrite(myMemoryStream.ToArray()); // myMemoryStream.WriteTo(Response.OutputStream); //works too Response.Flush(); Response.Close(); Response.End();

Return http 204 “no content” to client in ASP.NET MVC2

In MVC3 there is an HttpStatusCodeResult class. You could roll your own for an MVC2 application: public class HttpStatusCodeResult : ActionResult { private readonly int code; public HttpStatusCodeResult(int code) { this.code = code; } public override void ExecuteResult(System.Web.Mvc.ControllerContext context) { context.HttpContext.Response.StatusCode = code; } } You’d have to alter your controller method like so: [HttpPost] … Read more

setcookie, Cannot modify header information – headers already sent [duplicate]

The warning is clear. Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\test\index.php:9) in C:\xampp\htdocs\test\index.php on line 12 Cookies are sent in the HTTP response header. Since the HTML content already started, you cannot go back to the header and add the cookie. From http://php.net/setcookie: setcookie() defines a cookie to … Read more

XlsxWriter object save as http response to create download in Django

A little update on @alecxe response for Python 3 (io.BytesIO instead of StringIO.StringIO) and Django >= 1.5 (content_type instead of mimetype), with the fully in-memory file assembly that has since been implemented by @jmcnamara ({‘in_memory’: True}) ! Here is the full example : import io from django.http.response import HttpResponse from xlsxwriter.workbook import Workbook def your_view(request): … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)