How to cause a 404 from code ?

  • Thread starter Thread starter Si
  • Start date Start date
S

Si

Hi,

How can I create a 404 error from the code, which will look on the
client side _exactly the same_ as he was trying to access a non
existing page ?

I want to use to filter users.
e.g., in global.asax:

pseudo code:
=========================
Application_BeginRequest(..)
{
if Request.Url.LocalPath.Contains("admin")
{
// next 2 lines no good since client doesnt see the "404" page
Response.Status = 404;
Response.End
}
}
=========================

The above code is not good enough for me, since the client is not
redirected to see a 404 in the browser,
or redirected to the 404 page which is web.config

I can use "Response.Redirect("404.aspx") for the custom 404 page,
but I want a "real" 404.

Thanks for any help.
 
There is no "Real" 404 page.......
If user's browser has option "show friendly page" turned on in IE then user
will see it with one condition. Your output is less than 256 bytes (or 512,
do not remember).

Other than that it's everything you will output to the browser.
so just add Response.Write("404 page") 100 times and you will see it in a
browser.

George.
 
pseudo code:
=========================
Application_BeginRequest(..)
{
if Request.Url.LocalPath.Contains("admin")
{
// next 2 lines no good since client doesnt see the "404" page
Response.Status = 404;
Response.End
}
}
=========================

real code:
//=========================
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if Request.Url.LocalPath.Contains("admin")
{
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;
}
}
//=========================
 
Hi

Thanks for your answers.
The code you've suggested didnt result in causing the browser to show
a "404" page, just a blank page.
I've found a different way to do which I'm not is the correct\best
way, but it works:

if Request.Url.LocalPath.Contains("admin")
{
throw new HttpException(404,"");
}


Thanks for your help.
 
The code you've suggested didnt result in causing the browser to show
a "404" page, just a blank page.

Hmm - that's curious - it works fine for me...

What version of IIS are you using, AAMOI...?
 
Back
Top