How to cause a 404 from code ?

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.
 
G

George Ter-Saakov

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.
 
M

Mark Rae [MVP]

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;
}
}
//=========================
 
S

Si

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.
 
M

Mark Rae [MVP]

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...?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top