This project is read-only.

404 question

Sep 9, 2009 at 12:32 PM

Hi,

I'm having a problem I hope someone can help me with. I'm trying to return a 404 Not Found if an invalid URL is specified. Using the code below the browser waits forever.

private void OnHttpRequest(object source, RequestEventArgs args)
{
  IHttpClientContext context = null;

  try
  {
    context = (IHttpClientContext)source;
    IHttpRequest request = args.Request;

    if (request.Uri.AbsolutePath != "/foo")
    {
      context.Respond(HttpHelper.HTTP11, System.Net.HttpStatusCode.NotFound, "Not Found");
    }
    else
    {
      // Serve content, code for this works just fine.
    }
  }
  catch (Exception ex)
  {
    // Handle exception
  }
  finally
  {
    context.Close();
  }
}

Sep 11, 2009 at 7:48 AM

I would throw NotFoundException.

Nov 9, 2009 at 5:11 PM

I'm sorry for the dumb question, but I am handling all my stuffs in the RequestReceived event (HttpListener). I see the same problem as above in the browsers and throwing the NotFoundException within the event handler will break the program. All that in the IDE, debug mode.

Do I missing something?

Thanks in advance.