1

I have implemented custom errors in my asp.net mvc application by following this article. What I have noticed is that if I go to http://www.mysite.com/some-non-existent-controller-and-action I get my 404 error page as expected. However, looking at what happens with firebug, I see that I get a 302 Found response for the non-existent page, which then redirects to my custom error page which then returns with a 404 (and displays the custom error page). Is this right? I don't think the 302 that is first returned is very good especially from an SEO perspective, and that maybe I need to think again about how I have implemented this.

2
  • I never noticed this before, but I just confirmed that this happens on my MVC2 app with custom errors implemented. Commented Jun 24, 2010 at 23:48
  • Looking into some of the solutions, I do not believe you can use those and customErrors at the same time. I would like to figure out how to make the customErrors return a direct 404 without the 302. Is there a way to do this? Commented Jun 25, 2010 at 6:29

2 Answers 2

1

The best guide(i think) for handling 404s can be found in this answer. Basically there are multiple ways in which 404s can happen:

  1. No route exists - matched by the catch all rule.
  2. Matched route but not found a controller - for rules with dynamic controller names - {controller}/{action}/{parameter} rule.
  3. Found route, but didn't find action - handled through HandleUnknownAction override.
  4. Found route and action but couldn't convert parameters - matched by the catch all rule.

The linked answer basically sets up a controller that can be executed from any point in the code without rewriting the URL - which is what you want.

In addition, you should also think about handling unhandled exceptions and bad URLs (like the ones containing unsafe characters like angle brackets). I that particular case you have to rewrite the URL, otherwise you can't render the response at all. These particular requests are kind of tricky, i blogged about that here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this looks good. I will take a proper look and feedback
0

Did you follow the advice down towards the bottom of the page adding a "catch-all" route that maps to your "NotFound" action:

routes.MapRoute("Catch All", "{*path}",
    new { controller = "Error", action = "NotFound" });

If you make this the very last route you add, any "unknown" URLs will map directly to your "NotFound" action on the ErrorController and you can just return the "not found" view directly from there, no redirects required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.