Throwing a 404 when rewriting urls

Discussion in 'ASP .Net' started by IanW, Aug 8, 2008.

  1. IanW

    IanW Guest

    Hi,

    I'd just like some advice on the best way to throw a 404 when
    rewriting urls. The scenario is as follows:

    My url is /products/redwidgets

    This redirects fine to ~products.aspx?id=redwidgets

    I then do a db lookup to see if redwidgets exists, but it doesn't. At
    this point I want to throw a 404 error.

    Should I create a 404.aspx page and server.transfer to that, or is
    there some other way?

    Thanks.
    IanW, Aug 8, 2008
    #1
    1. Advertising

  2. IanW

    IanW Guest

    So Do I put this code in the products.aspx page?

    HttpContext.Current.Response.StatusCode = 404;
    HttpContext.Current.Response.SuppressContent = true;
    HttpContext.Current.ApplicationInstance.CompleteRequest();
    return;

    I have the following in web.config, but the current solution does not
    redirect to the actual error page. How would I accomplish that?

    <customErrors mode="Off">
    <error statusCode="404" redirect="~/404.aspx"/>
    </customErrors>

    On 8 Aug, 10:39, "Mark Rae [MVP]" <> wrote:
    > "IanW" <> wrote in message
    >
    > news:...
    >
    > > Should I create a 404.aspx page and server.transfer to that, or is
    > > there some other way?

    >
    > HttpContext.Current.Response.StatusCode = 404;
    > HttpContext.Current.Response.SuppressContent = true;
    > HttpContext.Current.ApplicationInstance.CompleteRequest();
    > return;
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net
    IanW, Aug 8, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sandeep pandit via .NET 247
    Replies:
    0
    Views:
    889
    sandeep pandit via .NET 247
    Apr 28, 2004
  2. Joel D. Kraft

    Non-existent page is not throwing 404 error

    Joel D. Kraft, May 24, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,469
    Joel D. Kraft
    May 25, 2005
  3. Dan
    Replies:
    0
    Views:
    340
  4. Smokey Grindle

    rewriting URLs?

    Smokey Grindle, Jun 21, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    354
    Alan Silver
    Jun 27, 2006
  5. Toni
    Replies:
    7
    Views:
    170
    Agile Consulting
    Feb 9, 2009
Loading...

Share This Page