3

I am trying to create a custom error page for 404 errors. I have implemented the Application_Error method in Global.asax.cs. For the most part this works great, but the method isn't getting called for URLs with 4 or more path segments.

The specific URL that I'm having trouble with is:

http://localhost/Area/Controller/Action/ID

If I remove the ID, my custom 404 age loads fine. The problem seems to be having 4 levels in my path (/Area/Controller/Action/ID). Any ideas?

Brian
  • 37,399
  • 24
  • 94
  • 109

3 Answers3

12

I suspect your routes don't account for a URL with 4 parts. Try adding a catch all route as your last route to test this:

routes.MapRoute("Error404", "{*url}", new {controller = "Error", action = "PageNotFound" } );

Including (some of) your routes in your RouteConfig.cs question might also help.

pim
  • 12,019
  • 6
  • 66
  • 69
Hector Correa
  • 26,290
  • 8
  • 57
  • 73
0

Set the custom error page for IIS: In IIS rightclick your website/virtual directory --> properties --> custom errors tab --> set the 404 error to your error page.

Your custom errors will only fire for things that fit your route rules. Once all of your route rules fail and it does not find a page at that path it passes the error up to IIS. IIS then uses its 404 rule for object not found so you have to override that and point it back into your project to your custom 404.

benjamin
  • 1,087
  • 7
  • 10
  • That doesn't sound like it would work. I need to get the request and routing information. From my understanding, the custom errors in IIS do a redirect. – Brian Aug 09 '11 at 02:16
-1

Are you getting your IIS error page instead? Try this.

  1. Open IIS, select your website, and choose the option "Error Pages".
  2. Find the 404 status code, right click and go to "Edit Feature Settings".
  3. Now, select "Custom Error pages" and change the "Path Type" to URL "Execute URL"
  4. Finally, put in your URL as a relative value, and now it should redirect to this url on 404 errors.
naveen
  • 53,448
  • 46
  • 161
  • 251
  • 1
    I can't seem to save the error setting. I'm getting a lock violation. I'll have to come back to it later. However, why would this be the problem? My 404 shows up correctly unless I have more than 3 path segments. If I do this, I'm concerned that I'll lose the Request object that caused the error in the first place. – Brian Aug 09 '11 at 02:31
  • @Brian: I had the same issue before hand. And what stuck on the lock violation. Didn't dig deep as the issue was not there on my production server. here is the question i asked on a similar issue. http://stackoverflow.com/questions/6290517/forcing-custom-404-pages-for-pages-in-url-routing – naveen Aug 09 '11 at 02:36