Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

How to cause a 404 from code ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Si - 26 Oct 2007 13:49 GMT
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.
George Ter-Saakov - 26 Oct 2007 13:58 GMT
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.

> Hi,
>
[quoted text clipped - 26 lines]
>
> Thanks for any help.
Mark Rae [MVP] - 26 Oct 2007 14:39 GMT
> pseudo code:
> =========================
[quoted text clipped - 8 lines]
> }
> =========================

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

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Si - 28 Oct 2007 11:23 GMT
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.
Mark Rae [MVP] - 29 Oct 2007 00:08 GMT
> 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...?

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.