4

I want to throw an 404 file not found exception from my module, but every exception is caught by DNN and does not show me my 404.aspx page (only the error page from DNN).

In my web.config I've added:

<httpErrors errorMode="Custom" defaultResponseMode="File">
  <remove statusCode="404" />
  <error statusCode="404" prefixLanguageFilePath="" path="/404.aspx" responseMode="ExecuteURL" />
</httpErrors>

&

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

Which work great when opening a page that does not exist. But trying to do the same thing with my module does not give me the same result...

I've tried the following without success:

throw new HttpException(404, "Not Found");
jerone
  • 16,206
  • 4
  • 39
  • 57
  • I'm having trouble trying to figure out why you would want the module to throw the 404? Is the module specifically designed to be the only thing on the page? – Chris Hammond Mar 13 '12 at 17:02
  • Yes it is. I'm developing a module for only my site and when an user does something weird, I just want an 404 to show up (which is my 404 page). – jerone Mar 13 '12 at 17:05

3 Answers3

1

Simple solution for DNN

TabInfo errorPage404 = new TabController().GetTabByName("404 Error Page", this.PortalId);
Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(errorPage404.TabID));
Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
1

just do this:

Response.StatusCode = 404;
Response.End(); 
erikvb
  • 104
  • 2
-1

Maybe you need to clear response first?

Response.Clear();
Response.StatusCode = 404;
Response.End(); 
Trapias
  • 94
  • 1
  • 7
  • This code does work, but it depends on the context of when it happens. In a normal get request for a page this will result in a 404, however if this occurs say inside a button click event in an ajax postback, the 404 will still be returned from the server, but the updatepanel will effectively ignore the 404 and it will appear as though nothing happened to the user. – ScottS Mar 14 '12 at 00:13
  • I've tried this code. My context is that I check in the page load for some conditions. If they don't match, I want to throw an 404. – jerone Mar 14 '12 at 07:27