9

I am tackling ASP.NET, MVC 3, web development, for the first time, all at the same time. Please bear with me, as I know this subject has been discussed heavily from different angles. I still have not found the answer to my specific question: Why doesn't my application find my Error.cshtml file when a 404 occurs, when it finds it just fine with other errors?

(Environment: Win 7 64bit, IIS7, SQL 2008 Express, VS2010, ASP.NET 4, MVC3, EF v4)

I have a controller, WorkerController.cs, that correctly reads and writes from the database. If I change the database name without updating my DbContext, it gives me an error. When I change web.config to always show custom errors, shows me the /Views/Shared/Error.cshtml file.

I do not have a FooController.cs file. If I go to /Foo, I get a 404 error, as expected. It tells me it cannot find the resource /Foo.

When I set customErrors mode="On" and make an http request to /Foo, I get a 404 error saying that /Error.cshtml cannot be found.

I am searching for and reading the posts that discuss the various methods of handling errors with designated controllers, but I really want to know what I'm missing. Why does it find /Error.cshtml for other errors, but not the 404 error?

JCii
  • 327
  • 2
  • 12

1 Answers1

8

Other than setting customErrors="On", have you defined a specific redirect for 404 errors?

If you have, say, an ErrorController setup your web.config, for instance, like:

<customErrors mode="On" defaultRedirect="/error/Problem">
    <error statusCode="404" redirect="error/FileNotFound"/>
</customErrors>

Or of you'd prefer static html pages for your errors:

<customErrors mode="On" defaultRedirect="Problem.html">
  <error statusCode="404" redirect="FileNotFound.html"/>
</customErrors>

You might want to take a look at this other question for some more information: ASP.NET MVC HandleError.

For improved error handling in MVC, though, you could also take a look at ELMAH (Error Logging Modules and Handlers):

Community
  • 1
  • 1
Sergi Papaseit
  • 15,999
  • 16
  • 67
  • 101
  • Sergi, thanks for the response. I tried the statusCode="404"with the static entry, but it still can't find it. Perhaps the error is bubbling up to IIS? – JCii Mar 10 '11 at 22:12
  • Hey JCii, did you mark your controller with the `HandleError` attribute? – Sergi Papaseit Mar 10 '11 at 22:26
  • I did, but that would be for a situation where the controller exists. I just created a new MVC3 web application and the only thing I modified was the web.config. I tried a couple of different versions: I tried /Views/Shared and /Shared for the Error.cshtml. I try going to a non-existant controller (http://localhost/foo) and I keep getting the YSOD 404 error... – JCii Mar 11 '11 at 02:47
  • Perhaps this: Note: The custom error page is only displayed when a request is made to a resource handled by the ASP.NET engine. ... By default, the IIS web server processes requests for static content like images and HTML files without invoking the ASP.NET engine. Consequently, if the user requests a non-existent image file they will get back IIS's default 404 error message rather than ASP.NET's configured error page. http://www.asp.net/hosting/tutorials/displaying-a-custom-error-page-cs – JCii Mar 11 '11 at 03:02
  • I wanted to add somehting along the lines of your second comment when I saw the url you are using to try and force the 404. If you go to localhost/foo then you'rew never going into your application so you'll always get a generic IIS error. In any case you should go to localhost/myapp/foo. May I ask what the YSOD says exactly? – Sergi Papaseit Mar 11 '11 at 08:31
  • @Jcii - Have you had any luck with this yet? – Sergi Papaseit Mar 16 '11 at 10:47
  • I was never able to feed a value into the redirect of the error definition in web.config so that IIS could find the error page. When I configured elmah, I saw that I was getting two re-direct errors: 1 for the localhost/foo and then another for /Error.cshtml. I setup a catch-all route in global.asax.cs that pointed to an ErrorController, then defined an action for the 404 error. That worked for Cassini in my development environment. For production on IIS, I additionally had to add 'Response.TrySkipIisCustomErrors = true;' to my Http404 action in the ErrorController to get the custom error. – JCii Mar 17 '11 at 18:26
  • @Jcii - If that's how you solved it, it might help others along if you post an aswer to your own question and mark it as the right answer. Glad you solved it btw. – Sergi Papaseit Mar 18 '11 at 08:34