Why does HttpWebRequest throw an exception instead returning HttpStatusCode.NotFound?

Ya this can be quite annoying when web pages use status codes heavily and not all of them are errors. Which can make processing the body quite a pain. Personally I use this extension method for getting the response. public static class HttpWebResponseExt { public static HttpWebResponse GetResponseNoException(this HttpWebRequest req) { try { return (HttpWebResponse)req.GetResponse(); … Read more

How do you return 404 when resource is not found in Django REST Framework

Simply way to do it, you can use raise Http404, here is your views.py from django.http import Http404 from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView from yourapp.models import Snippet from yourapp.serializer import SnippetSerializer class SnippetDetailView(APIView): def get_object(self, pk): try: return Snippet.objects.get(pk=pk) except Snippet.DoesNotExist: raise Http404 def get(self, request, pk, format=None): … Read more

Django staticfiles app help

I implore you to read the howto docs here: http://docs.djangoproject.com/en/dev/howto/static-files/ In short: STATIC_ROOT is only used if you call the collectstatic manangement command. It’s not needed to add the directory to the STATICFILES_DIRS setting to serve your static files! During development (when the automatic serving view is used) staticfiles will automatically look for static files … Read more

IIS7 custom 404 not showing

answer was to use <httpErrors existingResponse=”Replace” errorMode=”Custom”> <remove statusCode=”404″ subStatusCode=”-1″ /> <error statusCode=”404″ prefixLanguageFilePath=”” path=”/pages/404.aspx?he” responseMode=”ExecuteURL” /> </httpErrors> and not to have any system.web customErrors this worked for both .aspx and non .aspx requests. bizarrely this combination did not come up in any of the blog posts and stackoverflow answers I had investigated, it was … Read more

How do I force redirect all 404’s (or every page, whether invalid or not) to the homepage?

Setting the error page to the home page like this error_page 404 /index.html; has a small problem, the status code of the home page will be “404 not found”, if you want to load the home page with a “200 ok” status code you should do it like this error_page 404 =200 /index.html; This will … Read more